I'm trying to learn Rust, and trying to write some extremely simple web server code to do so.
I thought I had a good idea of the basics of lifetimes & borrowing in simple code, but I'm finding that either I'm missing a basic technique somewhere, or what I thought was a simple case is actually much more complicated for some reason.
What I'm essentially trying to do is this:
use std::env;
use std::convert::Infallible;
use std::net::SocketAddr;
use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};
// A demo web server: takes a message on the command-line, then
// serves it back to incoming requests.
#[tokio::main]
pub async fn main() {
let args: Vec<String> = env::args().collect();
let message = format!("Arguments were: {:?}", &args[1..]);
serve_message(message).await;
}
pub async fn serve_message(message: String) {
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let make_svc = make_service_fn(|_conn| {
async move {
Ok::<_, Infallible>(service_fn(move |_: Request<Body>| async move {
Ok::<_, Infallible>(
Response::new(Body::from(message))
)
}))
}
});
let server = Server::bind(&addr).serve(make_svc);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
}
This fails to compile with:
error[E0507]: cannot move out of `message`, a captured variable in an `FnMut` closure
--> src/main.rs:22:68
|
17 | pub async fn serve_message(message: String) {
| ------- captured outer variable
...
22 | Ok::<_, Infallible>(service_fn(move |_: Request<Body>| async move {
| ____________________________________________-----------------------_^
| | |
| | captured by this `FnMut` closure
23 | | Ok::<_, Infallible>(
24 | | Response::new(Body::from(message))
| | -------
| | |
| | variable moved due to use in generator
| | move occurs because `message` has type `String`, which does not implement the `Copy` trait
25 | | )
26 | | }))
| |_____________^ move out of `message` occurs here
error[E0507]: cannot move out of `message`, a captured variable in an `FnMut` closure
--> src/main.rs:21:9
|
17 | pub async fn serve_message(message: String) {
| ------- captured outer variable
...
20 | let make_svc = make_service_fn(|_conn| {
| ------- captured by this `FnMut` closure
21 | / async move {
22 | | Ok::<_, Infallible>(service_fn(move |_: Request<Body>| async move {
23 | | Ok::<_, Infallible>(
24 | | Response::new(Body::from(message))
| | -------
| | |
| | variable moved due to use in generator
| | move occurs because `message` has type `String`, which does not implement the `Copy` trait
25 | | )
26 | | }))
27 | | }
| |_________^ move out of `message` occurs here
I've tried a wide variety of more complex modifications of this, with cloning, ARCs, state into a struct with a handle impl, and lots of other approaches, but I'm struggling and each of them seems to bring me back to the same fundamental problem above. I'm clearly missing something essential about how async, closures & ownership interact, and the tools to manage that. I have seen How to re-use a value from the outer scope inside a closure in Rust? which is similar, but the only answer's example is a simpler demo that doesn't clearly translate to the larger problem - just adding .clone()
as suggested everywhere doesn't seem sufficient for this case.
The part I find most confusing is that this is extremely similar to one of Hyper's own examples: https://docs.rs/hyper/latest/hyper/service/fn.make_service_fn.html#example. But that example doesn't seem to hit any issues, while this does.
What's the correct & idiomatic way to do this, why does it work, and what's the difference between this and that Hyper example case? Beginner-level explanations much appreciated.
Arc
should work. How did you try it?