rocket cannot find macro `json` in this scope

79 views Asked by At

when I follow the tutorial to do when I found some video tutorial did not appear in the problem, hope to solve it.

Cargo.toml:

[package]
name = "hello_rocket"
version = "0.1.0"
edition = "2021"

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

[dependencies]
serde = "1.0.130"

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

This part is code:

use rocket::serde::json::{Json, Value};
use rocket::serde::{Deserialize, Serialize};
use rocket::{delete, get, post, put, routes};

#[get("/")]
async fn hello() -> String {
    "hello world!".to_string()
}

// restful
// get
#[get("/ex")]
async fn get_exs() -> Value {
    json!({    // error: cannot find macro `json` in this scope
        "res": "get_exs list"
    })
}

#[get("/ex/<id>")]
async fn get_ex(id: usize) -> String {
    format!("get_ex id: {}", id)
}

// post
#[post("/ex")]
async fn post_ex() -> String {
    "post_ex".to_string()
}

// put
#[put("/ex/<id>")]
async fn put_ex(id: usize) -> String {
    format!("put_ex: id: {}", id)
}

// delete
#[delete("/ex/<id>")]
async fn delete_ex(id: usize) -> String {
    format!("delete_ex: id: {}", id)
}

#[rocket::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    rocket::build()
        .mount("/hello", routes![hello])
        .mount(
            "/base",
            routes![get_exs, get_ex, post_ex, put_ex, delete_ex],
        )
        // 可以复用
        .mount(
            "/second",
            routes![get_exs, get_ex, post_ex, put_ex, delete_ex],
        )
        .launch()
        .await?;
    Ok(())
}

Error: cannot find macro `json` in this scope

Specifically JSON! This macro cannot be used

1

There are 1 answers

2
ricky On

You have a capital J in "Json" in your use statement. The macro is called "json".