如何解决Rust / Rocket:特征serde :: ser :: Serialize没有为struct实现
我正在尝试使用Rocket创建一个简单的终结点。我的Cargo.toml
具有以下依赖性:
[dependencies]
rocket = "0.4.2"
rocket_codegen = "0.4.2"
rocket_contrib = "0.4.2"
main.rs
如下:
#[macro_use]
extern crate rocket;
use rocket_contrib::json::Json;
use serde::Serialize;
#[get("/org")]
fn getorg() -> Json<Org> {
Json(Org {
id: Option::from(25),name: "Incredible-Customer".to_string(),})
}
#[derive(Serialize)]
pub struct Org {
pub id: Option<i32>,pub name: String,}
fn main() {
rocket::ignite().mount("/",routes![getorg]).launch();
}
编译会导致错误:
error[E0432]: unresolved import `serde`
--> src/main.rs:3:5
|
3 | use serde::Serialize;
| ^^^^^ use of undeclared crate or module `serde`
error: cannot determine resolution for the derive macro `Serialize`
--> src/main.rs:14:10
|
14 | #[derive(Serialize)]
| ^^^^^^^^^
|
= note: import resolution is stuck,try simplifying macro imports
error[E0658]: `macro` is experimental
--> src/main.rs:7:1
|
7 | #[get("/org")]
| ^^^^^^^^^^^^^^
|
= note: see issue #39412 <https://github.com/rust-lang/rust/issues/39412> for more information
= help: add `#![feature(decl_macro)]` to the crate attributes to enable
= note: this error originates in an attribute macro (in Nightly builds,run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `Org: serde::ser::Serialize` is not satisfied
--> src/main.rs:8:16
|
8 | fn getorg() -> Json<Org> {
| ^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `Org`
|
::: /Users/shep/.cargo/registry/src/github.com-1ecc6299db9ec823/rocket-0.4.5/src/handler.rs:202:20
|
202 | pub fn from<T: Responder<'r>>(req: &Request,responder: T) -> Outcome<'r> {
| ------------- required by this bound in `handler::<impl rocket::Outcome<rocket::Response<'r>,rocket::http::Status,rocket::Data>>::from`
|
= note: required because of the requirements on the impl of `Responder<'_>` for `rocket_contrib::json::Json<Org>
对于如何调查此错误,我感到非常困惑。这是一个依赖问题吗?为什么?我已经将rocket
依赖项版本化为相同的依赖项,但是显然,此serde
依赖项是令人不快的。谷歌搜索声称这是我的依赖项中的一个版本不匹配-但是我该如何自己解决这个问题?
解决方法
将serde = {version = "1.0",features = ["derive"]}
和serde_json = {version = "1.0"}
添加到您的cargo.toml
中,以便能够从Serialize
和Deserialize
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。