Reputation: 149
What is the error "cannot match against a tuple struct which contains private fields"? I'm getting an error like this for web::Path(id). How can I resolve it?
use actix_web::{get, post, web, HttpResponse,Responder};
use crate::model::todo::*;
#[get("/todos/{id}")]
async fn get_todo(web::Path(id): web::Path<u32>) -> impl Responder {
println!("get_todo");
let id_option: Option<u32> = Some(id);
HttpResponse::Ok().json(Todo {
id: id_option,
content: String::from("やること"),
completed: false,
})
}
#[post("/todos")]
async fn post_todo(todo: web::Json<Todo>) -> impl Responder {
println!("post_todo");
println!("{:?}", todo);
HttpResponse::Ok().body("ok")
}
Upvotes: 1
Views: 145
Reputation: 222278
The field of the struct actix_web::web::Path
is private so you cannot pattern match on it to extract the inner value; you need to call .into_inner()
. This should work:
#[get("/todos/{id}")]
async fn get_todo(id: web::Path<u32>) -> impl Responder {
println!("get_todo");
let id_option: Option<u32> = Some(id.into_inner());
...
}
Upvotes: 2