如何在Warp中创建可变路径?

时间:2019-07-05 02:25:29

标签: rust rust-warp

我正在尝试在Warp中使用可变路径。我已经尝试过了:

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));
}

但是我得到了错误:

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`

使path参数具有'static生存期的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

通过泄漏可以从分配的字符串中获取静态字符串。

let uuid_str = Box::leak(uuid.to_string().into_boxed_str());
let hello = warp::path(uuid_str).map(|| "hello world");