Can't make basic example of Serde Serialize work

Viewed 1128

Serde documentation says:

All of these can be serialized using Serde out of the box.

serde_json is just for the example, not required in general.

This is exactly what I need, a basic serialization of a struct into some basic binary format. I don't need JSON format, I want to keep it simple to be able to store struct in a file or send it to the network. The documentation is not clear on how to use serde for basic (binary or default) serialization, it only shows example with a JSON but this is not what I am looking for. I also don't want to implement my own serialize method, I want to use the default methods that Serde provides.

This is my example, so how do I make it work?

use serde::{Serialize, Deserialize,Serializer};

#[derive(Serialize, Deserialize, Debug)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let point = Point { x: 1, y: 2 };

    //let serialized = serde::serialize(&point).unwrap(); // <-- doesnt work!
    //let serialized = Serializer::serialize(&point); // <-- doesnt work!
    //let serialized = point.serialize(Serializer); // <-- doesn't work!
    println!("data = {:?}", serialized);
}

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b57a77399280f19664bb004201319b32

This is my dependency line:

[dependencies]
serde = { version = "1.0", features = ["derive"] }
1 Answers
Related