I don't know why but if I import Serialize like this use serde::ser::Serialize; I get error
error: cannot find derive macro `Serialize` in this scope
--> src/requester/mod.rs:14:10
|
14 | #[derive(Serialize)]
|
But if I import like use serde::Serialize; everything Ok.
i have same question also."cannot find derive macro Serialize in this scope"
my code:
#![feature(proc_macro_hygiene,decl_macro)]
#[macro_use] extern crate rocket;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Point{
x: Vec<f64>,
y: Vec<i32>,
z:Vec<String>,
}
#[get("/<code>/<startdate>/<enddate>")]
fn get_price(code: String, startdate: String,enddate:String) -> String {
let point = Point { x: vec![1.0f64,2.0,3.0], y: vec![2_i32,3,4,5],z:vec!["a".into(),"b".into(),"c".into(),"d".into()] };
let json_string = serde_json::to_string(&point).unwrap();
json_string
}
fn main() {
rocket::ignite().mount("/get_price", routes![get_price]).launch();
}
error:
error: cannot find derive macro `Serialize` in this scope
--> src\main.rs:5:11
|
5 | #[derive(Serialize, Deserialize, Debug)]
| ^^^^^^^^^
error: cannot find derive macro `Deserialize` in this scope
--> src\main.rs:5:22
|
5 | #[derive(Serialize, Deserialize, Debug)]
| ^^^^^^^^^^^
warning: unused imports: `Deserialize`, `Serialize`
--> src\main.rs:4:13
|
4 | use serde::{Serialize, Deserialize};
| ^^^^^^^^^ ^^^^^^^^^^^
|= note: `#[warn(unused_imports)]` on by default
error[E0277]: the trait bound `Point: serde::ser::Serialize` is not satisfied
--> src\main.rs:14:27
|
14 | let json_string = serde_json::to_string(&point).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `Point`
|
= note: required by `serde_json::ser::to_string`
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0277`.
error: Could not compile `rust_plot`.
Caused by:
process didn't exit successfully: `rustc --edition=2018 --crate-name rust_plot src\main.rs --color never --crate-type bin --emit=dep-info,link -C opt-
level=3 -C metadata=2fc579e68a7bb85b -C extra-filename=-2fc579e68a7bb85b --out-dir D:\rust_plot\target\release\deps -L dependency=D:\rust_plot\target\re
lease\deps --extern rocket=D:\rust_plot\target\release\deps\librocket-4ca9dd94486d7b84.rlib --extern rocket_contrib=D:\rust_plot\target\release\deps\lib
rocket_contrib-90137ffd80943fbe.rlib --extern serde=D:\rust_plot\target\release\deps\libserde-b09781ea79315232.rlib --extern serde_derive=D:\rust_plot\t
arget\release\deps\serde_derive-419b0f41938cb2c4.dll --extern serde_json=D:\rust_plot\target\release\deps\libserde_json-6da4c838b62aa43c.rlib -L native=
D:\rust_plot\target\release\build\ring-a35e82e2d3ca382a\out` (exit code: 1)
toml:
[dependencies]
rocket = { path = "D:/Rocket-master/Rocket-master/core/lib" }#"0.4.1"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
#hdf5 = "0.5"
rocket_contrib = { path = "D:/Rocket-master/Rocket-master/contrib/lib"}
D:\rust_plot>cargo tree
rust_plot v0.1.0 (D:\rust_plot)
โโโ serde v1.0.92
โโโ serde_derive v1.0.100
โ โโโ proc-macro2 v1.0.3
โ โ โโโ unicode-xid v0.2.0
โ โโโ quote v1.0.2
โ โ โโโ proc-macro2 v1.0.3 ()
โ โโโ syn v1.0.5
โ โโโ proc-macro2 v1.0.3 ()
โ โโโ quote v1.0.2 ()
โ โโโ unicode-xid v0.2.0 ()
โโโ serde_json v1.0.39
โโโ itoa v0.4.4
โโโ ryu v0.2.8
โโโ serde v1.0.92 (*)
other:
1.39 nightly +win10
thanks
@dmitryvakulenko
The serde::ser::Serialize is a trait, so you cannot use it in attributes as in #[derive(...)].
The serde::Serialize is _also_ a trait, but re-exported from the serde::ser path.
The serde::Serialize is _also_ a macro, but re-exported from the serde_derive crate. You _must_ enable the derive feature for the serde dependency in your manifest file for this to work.
@songroom2016
I think you should either use serde_derive when importing Serialize and Deserialize macros or enable the derive feature in your manifest file.
@dmitryvakulenko @songroom2016
For further clarification: https://serde.rs/derive.html.
it works.
use serde::{Serialize, Deserialize};
=>
use serde_derive::{Serialize, Deserialize};
thanks
@rustka, thank you for explanation!
This worked for me:
[dependencies]
serde = { version = "1.0", features = ["derive"] }
Found here: https://stackoverflow.com/questions/49113368/how-do-i-fix-cannot-find-derive-macro-in-this-scope
Most helpful comment
it works.
use serde::{Serialize, Deserialize};=>
use serde_derive::{Serialize, Deserialize};thanks