Can not understand why do not need clone for an async move block (example code from tower tutorial)

I'am reading this article Inventing the Service trait. Here is the code that difficult to understand for me.

trait Handler {
    type Future: Future<Output = Result<HttpResponse, Error>>;

    fn call(&mut self, request: HttpRequest) -> Self::Future;
}

impl Server {
    async fn run<T>(self, mut handler: T) -> Result<(), Error>
    where
        T: Handler,
    {
        let listener = TcpListener::bind(self.addr).await?;

        loop {
            let mut connection = listener.accept().await?;
            let request = read_http_request(&mut connection).await?;

            task::spawn(async move {
                // have to call `Handler::call` here
                match handler.call(request).await {
                    Ok(response) => write_http_response(connection, response).await?,
                    Err(error) => handle_error_somehow(error, connection),
                }
            });
        }
    }
}

My understanding is that task::spawn(async move {} move the handler to the async block because of handler.call(request), so this code can not compile. Is it true or my understanding is wrong?

Thanks for your explanation!

It wouldn't compile. It's either the author forget to test the code or it's just a half-pseudocode for demostration only.

You’re right we would need a clone there. The code the mainly intended to convey the idea and not be a working solution. With that said you’re welcome to submit a PR that fixes it!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.