Rust `error[E0382]: borrow of moved value` inside a Tokio `spawn`, and what fixed it
This one showed up in a small HTTP service I was hacking on at home, nothing fancy: accept a config, spawn a worker per request, hand each worker a list of rules. I'd been writing Go for a while and my instinct was to just close over the slice. Rust had other plans.
## The error I kept hitting
Compiling with Rust 1.79 and Tokio 1.38:
error[E0382]: borrow of moved value: rules
--> src/main.rs:24:24
|
12 | let rules = load_rules();
| ----- move occurs because rules has type Vec<Rule>, which does not implement the Copy trait
...
24 | tokio::spawn(async move {
| ----------- value moved here
25 | apply(&rules, req).await;
| ^^^^^^ value borrowed here after moveerror: aborting due to 1 previous error
I read that three times before it clicked. I was moving rules into the first spawn and then trying to use it again in the loop.
## Why it wasn't just a clone
The obvious fix is .clone() on every iteration, and that compiled, but each clone copied the whole Vec. With a few hundred rules and a few thousand requests that showed up in the allocator profile. The deeper issue is that tokio::spawn requires the future to be 'static, so a plain &Vec<Rule> borrowed from the caller can't be sent to the task at all. Even if the borrow checker allowed it, the task might outlive the function that created it.
## The version that actually worked
Wrap the shared data in an Arc once and clone the cheap handle:
use std::sync::Arc;let rules = Arc::new(load_rules());
for req in incoming {
let rules = Arc::clone(&rules);
tokio::spawn(async move {
apply(&rules, req).await;
});
}
Arc::clone bumps a refcount; it doesn't copy the rules. The 'static requirement is satisfied because the Arc owns its data.
## The second error that followed
Once that compiled I got a different one, which is how I knew I'd fixed the first:
error[E0277]: Rc<Vec<Rule>> cannot be sent between threads safely
--> src/main.rs:24:9
|
= note: required because it appears within the type {async block@...}
I'd reached for Rc first out of habit. Tokio's multi-thread runtime needs Send, and Rc isn't. Arc is. One-character change, different error entirely.
## What I took away
The E0382 message is accurate but it doesn't tell you the real constraint, which is the 'static bound on the spawned future. I stopped treating "clone it" as the answer and started asking what the task is allowed to own. The service has been up for a month now and the allocator graph is boring, which is exactly what you want.
In hindsight the compiler was trying to tell me the ownership story twice. The first error was about the borrow after the move; the second, once I used Arc, was about Send. Neither one names the real design question, which is who owns the rules and for how long. I ended up drawing it on a napkin, which is embarrassing but worked. The Arc lives as long as the last task holding it, and the tasks live as long as the request, so the lifetime falls out naturally. No manual bookkeeping, no unsafe, and no clone of the underlying data. I also turned on clippy::redundant_clone as a warning after this, so the next time someone reaches for clone out of habit, Clippy says something before it becomes a profile problem.