我似乎不能让提供的使用serde序列化结构的example工作。我正在为我的Address结构实现特征Serialize,但是我得到了一个编译错误,表明这个特征没有实现。我做错了什么?
[dependencies]
serde = "1.0.118"
serde_json = "1.0.60"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(())
}error[E0277]: the trait bound `Address: Serialize` is not satisfied
--> src\main.rs:21:35
|
21 | let j = serde_json::to_string(&address)?;
| ^^^^^^^^ the trait `Serialize` is not implemented for `Address`
|
::: C:\Users\Primary User\.cargo\registry\src\github.com-1ecc6299db9ec823\serde_json-1.0.60\src\ser.rs:2221:17
|
2221 | T: ?Sized + Serialize,
| --------- required by this bound in `serde_json::to_string`发布于 2020-12-18 01:43:39
您需要在Cargo.toml中为serde指定derive功能。
serde = { version = "1.0.118", features = ["derive"] }有关更多信息,请参阅:https://serde.rs/derive.html
https://stackoverflow.com/questions/65345581
复制相似问题