尽管 Rocket 和 serde 都列为依赖项,但无法使用 Rocket::serde::json::Json

kal*_*m42 8 rust rust-rocket

我按照快速入门指南进行操作。现在我试图返回一些超级简单的 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
Run Code Online (Sandbox Code Playgroud)

文件

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"
Run Code Online (Sandbox Code Playgroud)

main.rs

[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"
Run Code Online (Sandbox Code Playgroud)

如果您转到此处,您会发现这几乎是文档的直接复制/粘贴。我对 Rust 的了解不够,无法解决依赖错误。

Cha*_*ffy 16

需要在您json的.rocketCargo.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"]
Run Code Online (Sandbox Code Playgroud)

这记录在Rocket 源代码的注释中,该源代码在此处生成该文档

  • 我认为如果它只出现在源文件的注释中,那么称其为“记录”是“不确定的”。 (3认同)
  • @KevinAnderson 该评论包含在生成的文档中:[docs.rs](https://docs.rs/rocket/0.5.0-rc.1/rocket/serde/json/index.html) (3认同)