代码之家  ›  专栏  ›  技术社区  ›  softshipper

如何撰写曲速日志?

  •  0
  • softshipper  · 技术社区  · 5 年前

    我是一个新的生锈,并试图建立一个网络应用程序 https://docs.rs/warp/0.2.3/warp/index.html .

    #[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)  
    

    我做错什么了?

    0 回复  |  直到 5 年前
        1
  •  1
  •   Yannik Yeo    5 年前
        let hello = warp::path!("hello" / String)
        .with(cors)
        .with(log)
        .map(|name| format!("Hello, {}!", name));
    

    过滤传递到后续过滤数据。在您的代码中,map函数将从它前面的最后一个过滤器(即日志过滤器)接收数据,并将数据作为“name”传递,其类型为“warp::filters::log::internal::Logged”。此类型无法格式化!或打印!因为它不实现显示特性。

    过滤顺序很重要。将路径过滤器后面的map函数更改为接收路径参数“name”:

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