Reputation: 4349
I'm trying to have a variable path in Warp. I have tried this:
use uuid::Uuid;
use warp::{self, Filter};
fn main() {
let uuid = Uuid::new_v4();
println!("{}", uuid);
let hello = warp::path(&uuid.to_string()).map(|| "hello world");
warp::serve(hello).run(([127, 0, 0, 1], 8080));
}
but I get the error:
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:9:29
|
9 | let hello = warp::path(&uuid.to_string()).map(|| "hello world");
| ------------^^^^^^^^^^^^^^^^- - temporary value is freed at the end of this statement
| | |
| | creates a temporary which is freed while still in use
| argument requires that borrow lasts for `'static`
What is the best way to have the path parameter have a 'static
lifetime?
Upvotes: 2
Views: 768
Reputation: 74
You can get a static string from an allocated one by leaking it.
let uuid_str = Box::leak(uuid.to_string().into_boxed_str());
let hello = warp::path(uuid_str).map(|| "hello world");
Upvotes: 2