I was trying to build a simple server using Hyper when I wrote some code that had an error. I thought the Rust compiler's error felt less helpful than usual so I thought I'd ask about it here.
Here's the code: Rust error message "captured variable cannot escape `FnMut` closure body" · GitHub
The error is in the gist too, but it looks like this:
error: captured variable cannot escape `FnMut` closure body
--> src/main.rs:18:13
|
18 | / match (req.method(), req.uri().path()) {
19 | | (&Method::POST, "/foo") => {
20 | | // let data_map = Arc::clone(&data_map);
21 | | let response_future =
... |
36 | | )),
37 | | }
| |_____________^ returns a reference to a captured variable which escapes the closure body
|
= note: `FnMut` closures only have access to their captured variables while they are executing...
= note: ...therefore, they cannot allow references to captured variables to escape
error: aborting due to previous error
error: could not compile `test`.
To learn more, run the command again with --verbose.
There's no helpful text pointing out which captured variable is accidentally escaping, there's not even the usual E00XX error that I can use in rustc --explain
. Is there a reason the error message doesn't have this information?
Also, in this case I finally fixed it by doing what's in the comments in the code: cloning the Arc
and then using move
for the closure. Was that the right fix or did I misunderstand something?