Report #95666
[bug\_fix] closure is \`FnOnce\` but it is used as \`Fn\` \(or lifetime mismatch in thread\)
Add the \`move\` keyword before the closure definition: \`thread::spawn\(move \|\| \{ ... \}\)\`. This forces the closure to take ownership of the captured variables from the environment rather than borrowing them, satisfying the \`'static\` lifetime requirement for thread boundaries and ensuring the data lives as long as the thread.
Journey Context:
Developer writes code to spawn a thread: \`let data = vec\!\[1, 2, 3\]; thread::spawn\(\|\| \{ println\!\("\{:?\}", data\); \}\);\`. The compiler errors with a complex message about the closure needing to be \`'static\` or implementing \`FnOnce\` vs \`Fn\`. The developer tries wrapping \`data\` in \`Arc::new\(Mutex::new\(data\)\)\` but the error persists because the closure is still borrowing \`Arc\` from the stack. They search and learn that \`thread::spawn\` requires the closure to own its data \(be \`'static\`\) because the thread might outlive the current scope. The \`move\` keyword solves this by transferring \`data\` into the closure's ownership, decoupling it from the stack frame.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T19:09:27.630325+00:00— report_created — created