Answer#
thread::spawn requires the closure, and everything it captures, to be Send: safe to move to another thread. Some types are deliberately not Send. The compiler stops you at build time, which is the point: it prevents a data race.
The usual cause in a desktop program is Rc. It counts references without atomic operations, so two threads touching the count could corrupt it.
use std::rc::Rc;
use std::thread;
fn main() {
let label = Rc::new(String::from("status"));
let handle = thread::spawn(move || {
println!("{label}");
});
handle.join().unwrap();
}error[E0277]: `Rc<String>` cannot be sent between threads safely
--> src/main.rs:6:32
|
6 | let handle = thread::spawn(move || {
| ------------- ^------
| | |
| __________________|_____________within this `{closure@src/main.rs:6:32: 6:39}`
| | |
| | required by a bound introduced by this call
7 | | println!("{label}");
8 | | });
| |_____^ `Rc<String>` cannot be sent between threads safely
|
= help: within `{closure@src/main.rs:6:32: 6:39}`, the trait `Send` is not implemented for `Rc<String>`(File paths in the message were shortened by hand; the rest is as printed.)
Ways to fix it, depending on what you need:
- The worker only needs a copy of the data. Clone the plain value (
String, numbers, aVec) and move the clone in. - Both sides need the same data. Replace
RcwithArc, and add aMutex(orRwLock) if either side changes it. - The worker needs to change the UI. It cannot touch UI objects. Send a message through a channel and let the UI thread apply the change. Toolkit handles are often not
Sendfor the same reason; the toolkit's documentation says how to reach the UI from another thread (for example Slint's weak handle withinvoke_from_event_loop, or egui'sContext::request_repaint).
Common mistakes#
- Wrapping the type in
unsafe impl Sendto make the error go away. That hides the data race instead of removing it. - Moving a whole
Appstruct into the thread. Move the few fields the worker needs.
Related questions#
What does Sync mean?#
A type is Sync if a shared reference to it can be used from several threads. Arc<Mutex<T>> is Send and Sync when T is Send.
Sources#
- The Rust Book, "Extensible Concurrency with the Sync and Send Traits": https://doc.rust-lang.org/book/ch16-04-extensible-concurrency-sync-and-send.html (read 2026-12-05)
- Slint,
invoke_from_event_loop: https://docs.rs/slint/1.18.1/slint/fn.invoke_from_event_loop.html (read 2026-12-05)