带扭曲的根路径示例?

Root path example with warp?

这是新手入门的 rust warp 示例。它应该是“超级简单”,但它目前让我觉得超级愚蠢。

use warp::Filter;

#[tokio::main]
async fn main() {
    // GET /hello/warp => 200 OK with body "Hello, warp!"
    let hello = warp::path!("hello" / String)
        .map(|name| format!("Hello, {}!", name));

    warp::serve(hello)
        .run(([127, 0, 0, 1], 3030))
        .await;
}

我想在根路径上 运行 这个,定义如下:

let hello = warp::path!("" /).map(|| "Hello!");

但是宏不采用空路径名。我收到错误:

no rules expected the token `start`

我想“超级简单”对不同的人有不同的含义。

附录:

所以,我尝试了 Ivan C(下面的评论)来自 It doesn't work either. Applying

提到的解决方案
let hello = warp::path::end().map(|name| format!("Hello"));

依次导致此错误消息:

   [rustc E0599] [E] no method named `map` found for opaque type `impl warp::Filter+std::marker::Copy` in the current scope
method not found in `impl warp::Filter+std::marker::Copy`
note: the method `map` exists but the following trait bounds were not
satisfied: `impl warp::Filter+std::marker::Copy: std::iter::Iterator`

似乎只有在不需要根路由的情况下才可以使用 warp 路径进行路由,这只是一个表演障碍。

这不编译:

let hello = warp::path::end()
    .map(|name| format!("Hello"));

因为如果您不再动态匹配路由路径的任何部分,那么闭包中的 name 参数从何而来?如果你删除未使用的 name 参数,并且 format! 也是不必要的,那么它有效:

use warp::Filter;

#[tokio::main]
async fn main() {
    let hello = warp::path::end()
        .map(|| "Hello");

    warp::serve(hello)
        .run(([127, 0, 0, 1], 3030))
        .await;
}

访问 http://127.0.0.1:3030 现在生成 Hello