ea7
March 4, 2025, 2:02pm
1
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 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
Jesper
March 4, 2025, 3:11pm
4
Would be nice if cargo/the compiler could give an error message (enable feature derive) because this comes up now and again...
1 Like
kpreid
March 4, 2025, 3:13pm
5
It should be, since Rust 1.72:
master
← Noratrieb:dont-forgor-too-much-from-cfg
opened 09:58PM - 10 Mar 23 UTC
# Examples
## `serde::Deserialize` without the `derive` feature (a classic be… ginner mistake)
I had to slightly modify serde so that it uses explicit re-exports instead of a glob re-export. (Update: a serde PR was merged that adds the manual re-exports)
```
error[E0433]: failed to resolve: could not find `Serialize` in `serde`
--> src/main.rs:1:17
|
1 | #[derive(serde::Serialize)]
| ^^^^^^^^^ could not find `Serialize` in `serde`
|
note: crate `serde` has an item named `Serialize` but it is inactive because its cfg predicate evaluated to false
--> /home/gh-Nilstrieb/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.160/src/lib.rs:343:1
|
343 | #[cfg(feature = "serde_derive")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
344 | pub use serde_derive::{Deserialize, Serialize};
| ^^^^^^^^^
= note: the item is gated behind the `serde_derive` feature
= note: see https://doc.rust-lang.org/cargo/reference/features.html for how to activate a crate's feature
```
(the suggestion is not ideal but that's serde's fault)
I already tested the metadata size impact locally by compiling the `windows` crate without any features. `800k` -> `809k`
r? @ghost
1 Like
ea7
March 4, 2025, 3:27pm
6
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...