gpt4 book ai didi

rust - 如何撰写经向日志?

转载 作者:行者123 更新时间:2023-12-03 11:45:28 26 4
gpt4 key购买 nike

我在Rust中还很陌生,并尝试使用https://docs.rs/warp/0.2.3/warp/index.html构建Web应用程序。

我试过了:

#[tokio::main]
async fn main() {
// GET /hello/warp => 200 OK with body "Hello, warp!"

let cors = warp::cors()
.allow_origin("http://localhost:8090")
.allow_methods(vec!["GET", "POST", "DELETE"]);

let log = warp::log("dashbaord-svc");

let hello = warp::path!("hello" / String)
.with(cors)
.with(log)
.map(|name| format!("Hello, {}!", name));


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

编译器提示:
error[E0277]: `warp::filters::log::internal::Logged` doesn't implement `std::fmt::Display`
--> src/main.rs:16:43
|
16 | .map(|name| format!("Hello, {}!", name));
| ^^^^ `warp::filters::log::internal::Logged` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `warp::filters::log::internal::Logged`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required by `std::fmt::Display::fmt`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

我究竟做错了什么?

最佳答案

    let hello = warp::path!("hello" / String)
.with(cors)
.with(log)
.map(|name| format!("Hello, {}!", name));
过滤器传递给后续过滤器数据。在您的代码中,map函数将从最后一个过滤器(即日志过滤器)接收数据,并将数据作为“名称”传递,其类型为“warp::filters::log::internal::Logged”。此类型无法格式化!或打印!与{}一起使用,因为它没有实现显示特征。
过滤器顺序很重要。在路径过滤器之后立即更改map函数,以将路径参数接收为“名称”:
    let hello = warp::path!("hello" / String)
.map(|name| format!("Hello, {}!", name))
.with(cors)
.with(log);

关于rust - 如何撰写经向日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62107101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com