Using toml 0.8.20

I'm new to Rust with only 3 years experience with Go. Rust is very different and I'm lost. My first atempt was to write & read a TOML config file, similar to what I have in my Go projects. Two days of utter failure, so I resorted to copy pasting an example from toml - Rust but it doesn't compile - failing to recognise Serialize.

My Cargo.toml includes serde = "1.0.218" and toml = "0.8.20"

use serde::Serialize;
use toml;

fn main() {
    #[derive(Serialize)]
    struct Config {
        ip: String,
        port: Option<u16>,
        keys: Keys,
    }

    #[derive(Serialize)]
    struct Keys {
        github: String,
        travis: Option<String>,
    }

    let config = Config {
        ip: "127.0.0.1".to_string(),
        port: None,
        keys: Keys {
            github: "xxxxxxxxxxxxxxxxx".to_string(),
            travis: Some("yyyyyyyyyyyyyyyyy".to_string()),
        },
    };

    let toml = toml::to_string(&config).unwrap();
}

Obviously, I must be doing something wrong.. Please help.

Hi. Can you please provide your exact Cargo.toml and also share the exact error message you get from the compiler (after running cargo check)? Your example looks fine, and it just works:trade_mark: after I pasted in in the playground.

I would guess you didn't enable the derive feature of serde. try run the following command:

$ cargo add serde --features derive
2 Likes

Would be nice if cargo/the compiler could give an error message (enable feature derive) because this comes up now and again...

1 Like

It should be, since Rust 1.72:

1 Like

Nice response from all concerned. cargo add serde --features derive did the job.

I've fixed the last last line myself, from:

et toml_str = toml::to_string(&config).unwrap();

to:

let toml_str = toml::to_string(&config).unwrap();
println!("{}", toml_str);

Thank you for the help. If only the documentation was more complete...