0

So I was trying to follow the example from https://medium.com/sean3z/building-a-restful-crud-api-with-rust-1867308352d8 to build a simple REST API. Half way through, the rust compiler gives me the the following error: unresolved imports 'rocket_contrib::Json', 'rocket_contrib::Value' no 'Json' in the root

I can't seem to figure out what I am doing wrong.

Here is my Cargo.toml:

[package]
name = "rust-api-test"
version = "0.1.0"
authors = ["username"]
edition = "2018"

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

[dependencies]
rocket = "0.4.4"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"

[dependencies.rocket_contrib]
version = "0.4.4"
default-features = false
features = ["json"]

hero.rs:

#[derive(Serialize, Deserialize)]
pub struct Hero {
    pub id: Option<i32>,
    pub name: String,
    pub identity: String,
    pub hometown: String,
    pub age: i32
}

and main.rs:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
#[macro_use] extern crate serde_derive;

mod hero;
use hero::{Hero};

use rocket_contrib::{Json, Value};


#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

fn main() {
    rocket::ignite().mount("/", routes![index]).launch();
}

I get the error on line 10: use rocket_contrib::{Json, Value};



I followed Sven Marnach's advice and now it works. I changed my main.rs file to the following:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
#[macro_use] extern crate serde_derive;

mod hero;
use hero::{Hero};

use rocket_contrib::json::{Json, JsonValue};


#[post("/", data = "<hero>")]
fn create(hero: Json<Hero>) -> Json<Hero> {
    hero
}

#[get("/")]
fn read() -> JsonValue {
    json!([
        "hero 1", 
        "hero 2"
    ])
}

#[put("/<id>", data = "<hero>")]
fn update(id: i32, hero: Json<Hero>) -> Json<Hero> {
    hero
}

#[delete("/<id>")]
fn delete(id: i32) -> JsonValue {
    json!({"status": "ok"})
}

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

fn main() {
    rocket::ignite()
        .mount("/", routes![index])
        .mount("/hero", routes![create, update, delete])
        .mount("/heroes", routes![read])
        .launch();
}
MWhatsUp
  • 41
  • 6

1 Answers1

2

The example code was written for the now obsolete version 0.3.x of Rocket. You can't start new projects based on the old version anymore, since some of the dependencies have been yanked from crates.io. However, it's relatively easy to fix the example code – the import the compiler is complaining about isn't used anyway, so you can simply remove it. In version 0.4.x of rocket_contrib the rocket_contrib::Json struct has been moved to rocket_contrib::json::Json, so you can also import from the new location if you need it. The rocket_contrib::Value enum has been replaced with rocket_contrib::json::JsonValue, albeit with a different implementation, so you may need to adapt any uses to the new interface.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841