extern crate rocket; use rocket::{*, form::Form}; extern crate rocketdyntemplates; use…
- User
extern crate rocket; use rocket::{*, form::Form}; extern crate rocket_dyn_templates; use rocket_dyn_templates::{Template, context}; use rocket::fs::FileServer;
struct Article { content: String, }
#[get("/")] fn index() -> Template { Template::render("index", context!{}) }
#[get("/new")] fn new_view() -> Template { Template::render("new", context!{}) }
#[post("/api/new", data = "")] fn new(data: Form) -> String { format!("{}", data.content) }
#[launch] fn rocket() -> _ { build() .attach(Template::fairing()) .mount("/", routes![index, new_view]) .mount("/static", FileServer::from("static")) } Compiling txtforum v0.1.0 (/Users/alexkatkov/Programs/Rust/txtforum) error[E0277]: the trait bound
Article: FromFormField<'_>is not satisfied --> src/main.rs:22:14 | 22 | fn new(data: Form) -> String { | ^^^^^^^^^^^^^ the traitFromFormField<'_>is not implemented forArticle| = help: the following other types implement traitFromFormField<'v>: bool isize i8 i16 i32 i64 i128 usize and 38 others = note: required forArticleto implementFromForm<'_>= note: required forrocket::form::Form<Article>to implementFromData<'_>For more information about this error, try
rustc --explain E0277. error: could not compiletxtforum(bin "txtforum") due to previous error - Assistant
The error states that the
Articlestruct does not implement theFromFormFieldtrait, which is required for it to be used with theFormtype in Rocket. To fix this, you need to implement theFromFormFieldtrait forArticle.Here's an example implementation of the
FromFormFieldtrait forArticle:rust