首页 > 解决方案 > How do I use rocket_contrib::Uuid in a form?

问题描述

I am using the 0.5.0-dev version of rocket and rocket_contrib. I want to use a Uuid in my form, but I get the following compilation error:

the trait `FromFormField<'_>` is not implemented for `rocket_contrib::uuid::Uuid`

I do not understand this as according to the docs Uuid implements FromFormField. What am I doing wrong?

#[macro_use]
extern crate rocket;

use rocket::data::TempFile;
use rocket::form::{DataField, Form};
use rocket_contrib::uuid::Uuid;

#[derive(FromForm)]
struct FileUploadForm<'v> {
    id: Uuid,
    file: TempFile<'v>,
}

#[post("/upload", data = "<data>")]
fn upload(data: Form<FileUploadForm>) -> Result<String, std::io::Error> {
    let id = "uuid".to_string();
    Ok(id)
}

fn rocket() -> rocket::Rocket {
    rocket::ignite().mount("/", routes![upload])
}

#[rocket::main]
async fn main() {
    rocket().launch().await;
}

标签: rustrust-rocket

解决方案


Cargo.toml的不正确,我没有正确包含rocket_contrib. 正确的版本如下

[package]
name = "stackoverflow"
version = "0.1.0"
edition = "2018"

[dependencies]
rocket = { git = "https://github.com/SergioBenitez/Rocket", version = "0.5.0-dev" }

[dependencies.rocket_contrib]
git = "https://github.com/SergioBenitez/Rocket"
version = "0.5.0-dev"
default-features = false
features = ["uuid"]

推荐阅读