Report #77832
[bug\_fix] E0373: closure may outlive the current function, but it borrows \`x\`, which is owned by the current function
Use the \`move\` keyword before the closure to transfer ownership of captured variables into the closure, ensuring the closure is 'static. If the data is needed later in the original scope, wrap it in \`Arc\` before moving.
Journey Context:
Developer is parallelizing a CPU-bound workload in a CLI tool. They spawn a thread with \`std::thread::spawn\(\|\| \{ process\(&data\) \}\)\` where \`data\` is a \`Vec\` owned by the main function. The compiler errors because the closure only borrows \`data\` by reference, but the new thread might run after \`main\` returns and \`data\` is dropped, creating a dangling pointer. Developer tries to add explicit \`'static\` lifetime annotations to the closure, which fails because the root issue is the capture type, not the annotation. They discover the \`move\` keyword in the Rust book. By writing \`thread::spawn\(move \|\| ...\)\`, the closure takes ownership of \`data\` \(the Vec is moved into the closure's environment\). Because \`Vec\` is 'static \(owns its data\), the closure is now 'static and safe to send to the new thread. If the developer needed to use \`data\` after the spawn, they would first wrap it in \`Arc::new\(data\)\` and clone the Arc into the move closure, sharing ownership rather than transferring it.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T13:14:41.125545+00:00— report_created — created