>>103222451
In Rust this is just
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Element {
id: i32,
whatever: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct Drawer {
elements: Vec<Element>,
average_data: f32,
name: String,
}
use bincode::{serialize, deserialize};
use std::fs::File;
use std::io::{Write, Read};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create sample data
let drawer = Drawer {
elements: vec![
Element { id: 1, whatever: "First".to_string() },
Element { id: 2, whatever: "Second".to_string() },
],
average_data: 3.14,
name: "My Drawer".to_string(),
};
// Serialize
let encoded: Vec<u8> = serialize(&drawer)?;
// Write to file
let mut file = File::create("drawer.bin")?;
file.write_all(&encoded)?;
// Read from file
let mut file = File::open("drawer.bin")?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
// Deserialize
let decoded: Drawer = deserialize(&buffer)?;
println!("Deserialized data: {:?}", decoded);
Ok(())
}