How to save variables in Rust like Python's pickle

I know python very well and I love the pickle module in python.
Which saves variables.

CAN I DO THIS IN RUST

1 Like

Serde is a very popular serialization library for Rust, you can look it up.

1 Like

I’m not familiar with Python more than the very basics and not familiar with pickle at all, but given the topic is “serialization”—depending on your specific use case—you might be able to do what you want with serde.

1 Like

As an aside, please don't shout in ALL CAPS as it's considered rude. You can use _underscores_ for italic text and **asterisks** for bold. In general, this forum uses the Markdown markup language, so you can even write embedded code (using `single` or ```triple``` backticks), bulletpoint or numbered lists, and much more.

8 Likes

As someone who’s written a lot of Python and Rust, you can definitely use serde in place of pickle for most anything you’d want to use it for. serde doesn’t have a default file format though so you’d have to pick one for your use case (such as JSON using serde_json).

2 Likes

There is even serde-pickle that allows you to read and write Pythons pickle format.

10 Likes

Hey I am having an issue with using serde with RustC not Cargo

 rustc -o main main.rs
error[E0463]: can't find crate for `serde`
 --> main.rs:1:1
  |
1 | extern crate serde;
  | ^^^^^^^^^^^^^^^^^^^ can't find crate

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.

Don't use rustc directly. It will not work with Cargo dependencies unless you manually do all the work that Cargo does, and that's a lot. Everyone just uses Cargo.

3 Likes

Yeah, I used Python and pickle before, but Serde is amazing :sparkles:.

With serde you can write your Rust structure once and serialize to a massive list of formats some of which include:

  • Python's pickle format
  • YAML
  • JSON
  • TOML
  • CBOR
  • and more

And, yes, use Cargo. It's great.

2 Likes

Ok I was just asking because I use repl.it for testing stuff and do the light stuff

1 Like

https://play.rust-lang.org has popular Cargo crates available, including serde.

4 Likes

Ah, gotcha. I haven't use repl.it before, but a possibly similar web tester that will work with Serde is the Rust playground: serde example.

Ok I got it Thanks
How do I close this issue

You don't really need need to close it, but if you want you can mark one of the replies as the "solution" so that it gets a green checkmark on it and shows as "answered" adjacent to the title.

1 Like

Thanks and Good Night

3 Likes

You cannot do what pickle do in Rust because pickle heavily use Pythons runtime, reflexion and ability to serialize even code (so you can serialize some object and deserialize it even in module without used classes). But this pickle properties costs in very slow runtime and security vulnerabilities.

In Rust, you need to enable serialization directly using, for example, serde which suggested above.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.