Json: cannot find derive macros and error[E0277]

Created on 9 Sep 2019  路  4Comments  路  Source: serde-rs/json

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 Serialize in this scope
--> src/main.rs:4:10
|
4 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^

error: cannot find derive macro Deserialize in this scope
--> src/main.rs:4:21
|
4 | #[derive(Serialize, Deserialize)]

error[E0277]: the trait bound Address: serde::ser::Serialize is not satisfied
--> src/main.rs:22:13
|
22 | let j = serde_json::to_string(&address)?;
| ^^^^^^^^^^^^^^^^^^^^^ the trait serde::ser::Serialize is not implemented for Address
|
= note: required by serde_json::ser::to_string

Cargo.toml:

[dependencies]
serde = "1.0.100"
serde_json = "1.0.40"

Most helpful comment

Documentation still needs to be corrected, faced the very same problem. its 2020 now

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vityafx picture vityafx  路  6Comments

elliottslaughter picture elliottslaughter  路  5Comments

selaux picture selaux  路  7Comments

AerialX picture AerialX  路  5Comments

dtolnay picture dtolnay  路  3Comments