{"id":1181,"hash":"6d38ebbf5e179e6688838980771308723da2e6a9a8e653a8890d8f560d5dcf0f","pattern":"Why Am I Getting &quot;Cannot Derive Macro In This Scope&quot;?","full_message":"Attempting cargo build against this code:\n\n#![allow(unused)]\n\nuse serde::{Deserialize, Serialize};\nuse serde_json::{Result, Value};\n\n#[derive(Serialize, Deserialize,Debug)]\nstruct Repository{\n    r#type: String,\n    url: String,\n}\n\nfn main() {\n    println!(\"Hello, world!\");\n}\n\nAnd here's the cargo.toml file:\n\n[package]\nname = \"demo_err\"\nversion = \"0.1.0\"\nauthors = [\"Onorio Catenacci <catenacci@ieee.org>\"]\nedition = \"2018\"\n\n# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html\n\n[dependencies]\nserde = \"1.0.104\"\nserde_json = \"1.0.44\"\n\nOf course my real code is a bit larger but that's the smallest bit of code with which I can reproduce the error.\n\nI get the following errors:\n\n   Compiling demo_err v0.1.0 (U:\\skunkworks\\rust\\demo_err)\nerror: cannot find derive macro `Serialize` in this scope\n --> src\\main.rs:9:10\n  |\n9 | #[derive(Serialize, Deserialize,Debug)]\n  |          ^^^^^^^^^\n\nerror: cannot find derive macro `Deserialize` in this scope\n --> src\\main.rs:9:21\n  |\n9 | #[derive(Serialize, Deserialize,Debug)]\n  |                     ^^^^^^^^^^^\n\nNow I would assume I've done something wrong--except for this sample code from serde_json. It's like this:\n\nuse serde::{Deserialize, Serialize};\nuse serde_json::Result;\n\n#[derive(Serialize, Deserialize)]\nstruct Person {\n    name: String,\n    age: u8,\n    phones: Vec<String>,\n}\n\nNow, one obvious difference is the r#type but using a different name doesn't fix the error.  Another obvious difference is serde_json::{Result, Value} but removing Value doesn't fix the error either.\n\nObviously something is different between my code and that sample but for the life of me I cannot figure out what's different.  Can someone please point me in the right direction?\n\nEDIT:\n\nYes I am aware of another question that is solved by the same required feature.  However the error message presented in this case is not the same as the error presented in the other case.  I wouldn't expect someone to be able to translate the two different error scenarios into the same solution.  Please leave this open.\n\nSpecifically this:\n\nerror: cannot find derive macro Serialize in this scope\n\nwhich is the main thing I'm asking about vs. this\n\nwarning: unused imports: Deserialize, Serialize\n\nWhich is the main point of the other question.","ecosystem":"cargo","package_name":"serde","package_version":null,"solution":"You need to activate the required feature to use derive macros. You can do this by changing serde declaration in cargo.toml :\n\nserde = { version = \"1.0.104\", features = [\"derive\"] }\n\nor via cargo command:\n\ncargo add serde --features derive\n\nFor more information please follow: https://serde.rs/derive.html\n\nSee also :\n\nHow do I fix \"cannot find derive macro in this scope\"?","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/60676916/why-am-i-getting-cannot-derive-macro-in-this-scope","votes":25,"created_at":"2026-04-19T04:52:32.441168+00:00","updated_at":"2026-04-19T04:52:32.441168+00:00"}