I tried to implement the Responder trait for "MyConfig" struct. But I don't know how to create Response (including the body whose type is String) which can be return from function.
I tried this code.
#[derive(Debug)]
struct MyConfig {
body: String,
}
impl<'r> Responder<'r> for MyConfig {
fn respond_to(self, _req: &Request) -> response::Result<'r> {
let body: String = self.body.clone();
Response::build()
.status(Status::Ok)
// .raw_header("Access-Control-Allow-Origin", "*")
.sized_body(Cursor::new(body.as_str()))
.ok()
}
}
This can't compile, and shows this error.
error[E0515]: cannot return value referencing local variable `body`
--> src/main.rs:53:9
|
53 | / Response::build()
54 | | .status(Status::Ok)
55 | | // .raw_header("Access-Control-Allow-Origin", "*")
56 | | .sized_body(Cursor::new(body.as_str()))
| | ---- `body` is borrowed here
57 | | .ok()
| |_________________^ returns a value referencing data owned by the current function