2

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
carloabelli
  • 4,289
  • 3
  • 43
  • 70
  • 1
    Maybe you should ask your question [in this form](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4fe049fafd738f32bc64a7fd490ebbd7) to let people test your code easily – Ömer Erden Jul 05 '19 at 05:43
  • Possible duplicate of [Getting "temporary value dropped while borrowed" when trying to update an Option<&str> in a loop](https://stackoverflow.com/questions/54488127/getting-temporary-value-dropped-while-borrowed-when-trying-to-update-an-option) – E_net4 Jul 05 '19 at 08:57

1 Answers1

2

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");
Manu
  • 76
  • 1
  • 4