Simple warp filter does not compile

RUST noob here.
I am trying to create a simple Warp filter that can give me the remote ip address of the caller. I got this from here
#![deny(warnings)]

use std::net::SocketAddr;
use warp::Filter;
#[tokio::main]

async fn main() {
    let addrfilter = warp::addr::remote()
        .map(|addr: Option<SocketAddr>| {
            println!("remote address = {:?}", addr);
        });
    warp::serve(addrfilter).run(([127, 0, 0, 1], 3030)).await;
}

When I compile this, I get the following errors

$ cargo build
   Compiling hello-warp v0.1.0 (E:\rust_projects\hello-warp)
error[E0277]: the trait bound `(): Reply` is not satisfied
  --> src\main.rs:12:17
   |
12 |     warp::serve(addrfilter).run(([127, 0, 0, 1], 3030)).await;
   |                 ^^^^^^^^^^ the trait `Reply` is not implemented for `()`
   | 
  ::: C:\Users\rokugver\.cargo\registry\src\github.com-1ecc6299db9ec823\warp-0.3.1\src\server.rs:26:17
   |
26 |     F::Extract: Reply,
   |                 ----- required by this bound in `serve`
   |
   = note: required because of the requirements on the impl of `Reply` for `((),)`

error[E0277]: the trait bound `(): Reply` is not satisfied
  --> src\main.rs:12:29
   |
12 |     warp::serve(addrfilter).run(([127, 0, 0, 1], 3030)).await;
   |                             ^^^ the trait `Reply` is not implemented for `()`
   |
   = note: required because of the requirements on the impl of `Reply` for `((),)`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0277`.
error: could not compile `hello-warp`
To learn more, run the command again with --verbose.

What am I missing here considering that I am using a documented example almost as it is?

Warp expects your serve filter to output something that implements Reply, so that it can know how to respond to requests. If you make your map closure return a Reply value (like a http::StatusCode or a String), your code should work:

let addrfilter = warp::addr::remote()
        .map(|addr: Option<SocketAddr>| {
            println!("remote address = {:?}", addr);
            StatusCode::OK
        });

Thanks, returning a string worked.

1 Like