I am running the Creating JSON by serializing data structures example code from README.md exactly as written in the documentation, with the exception of a main() function that calls print_an_address():
main.rs:
use serde::{Deserialize, Serialize};
use serde_json::Result;
#[derive(Serialize, Deserialize)]
struct Address {
street: String,
city: String,
}
fn main() {
print_an_address();
}
fn print_an_address() -> Result<()> {
// Some data structure.
let address = Address {
street: "10 Downing Street".to_owned(),
city: "London".to_owned(),
};
// Serialize it to a JSON string.
let j = serde_json::to_string(&address)?;
// Print, write to a file, or send to an HTTP server.
println!("{}", j);
Ok(())
}
When this code runs, the following errors are logged:
error: cannot find derive macro
Serializein this scope
--> src/main.rs:4:10
|
4 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^
error: cannot find derive macro
Deserializein this scope
--> src/main.rs:4:21
|
4 | #[derive(Serialize, Deserialize)]
error[E0277]: the trait bound
Address: serde::ser::Serializeis not satisfied
--> src/main.rs:22:13
|
22 | let j = serde_json::to_string(&address)?;
| ^^^^^^^^^^^^^^^^^^^^^ the traitserde::ser::Serializeis not implemented forAddress
|
= note: required byserde_json::ser::to_string
Cargo.toml:
[dependencies]
serde = "1.0.100"
serde_json = "1.0.40"
This is resolved by explicitly including the derive feature in Cargo.toml:
[dependencies]
serde = { version = "1.0.100", features = ["derive"] }
serde_json = "1.0.40"
This looks like a documentation defect
@GregWoods Yes, the documentation should probably specify that the "derive" feature needs to be explicitly specified.
Documentation still needs to be corrected, faced the very same problem. its 2020 now
Most helpful comment
Documentation still needs to be corrected, faced the very same problem. its 2020 now