A lot of Rust’s thread safety is two auto traits: Send (OK to move to another thread) and Sync (OK to share as &T). The compiler checks them at thread::spawn and when references cross threads.
Send
Take the code below: we try to move a reference-counted String (Rc<String>) into another thread. The compiler rejects it.
1 | use std::{rc::Rc, thread}; |
Read the error and the relevant trait impls:
1 | impl<T: ?Sized> !Send for Rc<T> {} |
thread::spawn requires the closure F to be Send. Rc is not Send, so the build fails.
Rc is a non-atomic reference count. Sharing it across threads can corrupt the count, so moving it between threads is not considered safe.
What does Send do?
Send is a marker trait: types that implement it can have their ownership transferred between threads safely — you can move the value to another thread.
There are no methods to implement yourself. Most primitives are Send. For composite types, if every field is Send, the whole type is automatically Send as well. (Sync works the same way as an auto trait.)
Anything you need to pass by value into concurrent code must be Send, or the compiler stops you.
Safety checks become trait bound checks at compile time instead of surprises at runtime. Nice trick.
Sync
Next, share the same Rc<String> with a thread without moving it — only borrow it in the closure. It still fails.
1 | use std::{rc::Rc, thread}; |
Rc is not Sync, so the compiler errors again.
1 | impl<T: ?Sized> !Sync for Rc<T> {} |
Sync is also a marker trait. It means the type can be shared across threads via shared references.
Sharing here means &T is safe to use concurrently. The rule of thumb: if &T is Send, then T is Sync.
So for concurrent shared access through &T, T must be Sync, or you get a compile error.
Again, trait bounds encode reference safety at compile time.
In short:
Send: safe to transfer ownership (move) between threads.Sync: safe to share references (&T) between threads.
For more depth, the official book and Nomicon sections are worth a read:
Questions? Leave a comment below or email: newbvirgil@gmail.com![]()
