rust - 如何在 Warp 中创建变量路径?

标签 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`

让路径参数具有'static生命周期的最佳方法是什么?

最佳答案

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

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

关于rust - 如何在 Warp 中创建变量路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56895824/

相关文章:

arrays - Rust 中 array::len 的运行时复杂度是多少?

slice - 如何从一个切片创建两个新的可变切片?

logging - 如何在warp中记录请求/响应主体?

http - 如何同时使用 Rejection 和问号运算符来处理 Warp 中的错误?

rust - 如何编写一个宏来返回基于字符串的结构的实现方法?

rust - 我如何实现 std::convert::From 使其不消耗其输入?

rust - 函数返回一个闭包,闭包返回一个使用环境变量的闭包

websocket - 在BusReader和Warp WebSocket接收器之间转发消息会留下未占用的缓冲区?

rust - 如何正确处理 Warp 路由的错误