Try to understand serialize and desirialize in Rust

I used Julia a lot, and there is a official package about serializing, which can serialize and deserialize any objects in Julia. In Rust I found that if I need to do the same thing in Julia I have to use different packages togother and add macros statement everywhere, which seems to make the code more unreadable.
But it seems that Rust can do the same thing as Julia do in serializing, so I'm confused that why Rust did not integerate serializing as a part of itself so that people do not have to add macros in their codes?

Without any prior knowledge about Julia, the Wikipedia description of

Julia is a high-level, dynamic programming language.

and the serialization description of

Write an arbitrary value to a stream in an opaque format, such that it can be read back by deserialize. The read-back value will be as identical as possible to the original, but note that Ptr values are serialized as all-zero bit patterns (NULL).

makes me highly doubt that their approach to serialization could be of any use in Rust.

As mentioned above, I don’t know anything about Julia, so this is just guesswork on my end.


As for the de-facto-standard way of doing serialization in Rust, look into serde, if you haven’t already.

3 Likes

Hmm, I have trouble comparing the serialization concepts in both languages. Julia's implementation serializes into some opaque binary format that can be interpreted by the deserialize method. This reminds me somewhat of the pickle format in Python, which is a format basically only used when passing data between python processes. On the other hand, Rust's https://serde.rs allows you to write your own serializers for any serialization format. So you can actually annotate your struct once with the derive macros and attributes and (de-)serialize your type into a ton of different formats, like Json [1], CBOR, Toml, you name it.

Also, Rust's standard library is small and pretty low-level by design, whereas Julia's contains more (high-level) functionality. It is very common in Rust to rely on community packages like serde for functionality you get from standard libraries in other languages.


  1. You need a 3rd party package for Json in Julia, too, by the way ↩︎

3 Likes

Slapping a #[derive(Serialize)] on types you want to be serializable does not count as "unreadable". Idiomatic Rust uses a lot of derive macros. It's a lot better than trait impls appearing magically out of nowhere (or disappearing unexpectedly, once the type no longer qualifies for the given trait). Auto traits are already a pain, there shouldn't be more of them.

Again, as explained above, "more magic" and "shorter code" does not mean "more readable". Number of characters is a terrible measure of code complexity and maintainability.

You are approaching this entirely from the wrong direction. The question shouldn't be "why isn't it in the language?"; it should be "why should it be in the language?" instead. If something can be implemented by a macro instead of being a core language feature, then it should not be built into the language. There are multiple good reasons for this, flexibility/exensibility and future-proofness being the two biggest ones, perhaps (the language can't be modified by 3rd parties, but anyone can write a derive macro; and once something is in the language, it stays there forever due to backward compatibility).

3 Likes