Report #92899
[bug\_fix] use of moved value: \`vec\` \[E0382\]
Clone the value before the move using \`let vec2 = vec.clone\(\);\` and move the clone into the closure, or use \`Arc>>\` if both threads need access, or restructure to use channels to transfer ownership back. Root cause: The \`move\` keyword in closures or function arguments transfers ownership \(moves\) the value; the original variable is invalidated to prevent use-after-move and ensure a single owner responsible for dropping.
Journey Context:
Developer creates a vector and spawns a thread to process it: \`let vec = vec\!\[1, 2, 3\]; thread::spawn\(move \|\| \{ println\!\("\{:?\}", vec\); \}\); println\!\("\{:?\}", vec\);\`. The compiler errors on the second \`println\!\` with 'use of moved value: vec'. The developer is confused because they thought \`move\` only affected the closure's internal scope. They search the error code E0382 and find explanations about Rust's move semantics. They realize that \`thread::spawn\` requires the closure to be \`'static\`, meaning it cannot borrow from the surrounding stack. The \`move\` keyword forces the closure to take ownership of \`vec\` by moving it into the new thread's stack. Once moved, the original \`vec\` binding in the main thread becomes invalid to prevent use-after-free \(since two threads cannot both own the memory and try to drop it\). They initially try to use \`&vec\` in the closure without \`move\`, which fails because the closure would outlive the main function scope. They consider using \`Box::leak\` to create a static reference, but realize this wastes memory. Finally, they decide to clone the vector before spawning \(\`let vec\_for\_thread = vec.clone\(\);\` and move \`vec\_for\_thread\`\), allowing the main thread to retain its copy while the worker thread processes its own. For larger shared state, they note that \`Arc>\` would be the appropriate pattern to share ownership rather than moving it.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T14:31:00.547520+00:00— report_created — created