Snoop Catt
Snoop Catt

Reputation: 2329

How do I save structured data to file?

My program contains a huge precomputation with constant output. I would like to avoid running this precomputation in the next times I run the program. Thus, I'd like to save its output to file in the first run of the program, and just load it the next time I run the program.

The output contains non-common data types, object and structs I defined myself.

How do I go about doing that?

Upvotes: 12

Views: 21938

Answers (2)

Heep
Heep

Reputation: 131

You would want to use something like serde to serialize the data, save it to disk, and then restore it from there on the next run. In particular, bincode, is useful to serialize the data in the binary format which saves much more space than JSON or other human readable format. But, you have to be careful not to use old serialized data if you have changed the layout of the structures in your program.

For more in depth, I would go to Serde's documentation, but the basic idea is to mark all your structures that need saving with #[derive(Serialize, Deserialize)], and use bincode to do the serialization/deserialization. Playground link for an example I wrote using serde_json (as bincode is not available in rust playground), but bincode is no different, other than using serialize and deserialize as opposed to to_vec and from_slice.

Upvotes: 6

Kitsu
Kitsu

Reputation: 3445

A de-facto standard way for (de-)serializing rust objects is serde. Given a rust struct (or enum) it produces an intermediate representation, which then can be converted to a desired format (e.g. json). Given a struct:

use serde::{Serialize, Deserialize};

// here the "magic" conversion is generated
#[derive(Debug, Serialize, Deserialize)]
struct T {
    i: i32,
    f: f64,
}

you can get a json representation with simple as oneliner:

let t = T { i: 1, f: 321.456 };
println!("{}", serde_json::to_string(&t).unwrap());
// prints `{"i":1,"f":321.456}`

as well as converting back:

let t: T = serde_json::from_str(r#"{"i":1,"f":321.456}"#).unwrap();
println!("i: {}, f: {}", t.i, t.f);
// prints i: 1, f: 321.456

Here is a playground link. This is an example for serde_json usage, but you may find other more suitable libraries like serde_sbor, serde_yaml, bincode, serde_xml and many many others.

Upvotes: 13

Related Questions