2

我遵循了快速入门指南。现在我正在尝试返回一些超级简单的 JSON,但文档是错误的,如果不进入 IRC,就无法提交票证。

错误

error[E0432]: unresolved import `rocket::serde::json`
 --> src/main.rs:2:20
  |
2 | use rocket::serde::json::Json;
  |                    ^^^^ could not find `json` in `serde`

For more information about this error, try `rustc --explain E0432`.
error: could not compile `my-api` due to previous error

文件

Cargo.toml

[package]
name = "my-api"
version = "0.1.0"
edition = "2021"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rocket = "0.5.0-rc.1"
serde = "1.0.130"

main.rs

#[macro_use] extern crate rocket;
use rocket::serde::{Serialize, json::Json};

#[derive(Serialize)]
struct Location {
    lat: String,
    lng: String,
}

#[get("/?<lat>&<lng>")]
fn location(lat: &str, lng: &str) -> Json<Location> {
    Json(Location {
        lat: 111.1111.to_string(),
        lng: 222.2222.to_string(),
    })
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![location])
}

如果你去这里,你会看到这几乎是文档的直接复制/粘贴。我对 Rust 的了解不够,无法解决依赖错误。

4

2 回答 2

4

json需要rocket在您的Cargo.toml.

[package]
name = "my-api"
version = "0.1.0"
edition = "2018"  // cut back for testing with nixpkgs-provided rust
publish = false

[dependencies]
serde = "1.0.130"

[dependencies.rocket]
version = "0.5.0-rc.1"
features = ["json"]

这在生成文档的 Rocket 源代码中的注释中有记录。

于 2021-11-15T01:41:53.000 回答
1

除了查尔斯的回答,我会将 serde 导入更改为:

serde = { version = "1.0", features = ["derive"] }

如此处所述

于 2021-11-15T01:57:45.223 回答