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.


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.


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).


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


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

 rustc -o main
error[E0463]: can't find crate for `serde`
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.


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.


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

1 Like has popular Cargo crates available, including serde.


Ah, gotcha. I haven't use 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


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.