Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Core Traits

The Send and Sync traits

Reference for Rust Send and Sync: what the auto traits promise, which std types lack them (Rc, RefCell, raw pointers), manual unsafe impls and common compiler errors.

Send and Sync are marker traits with no methods. They tell the compiler what is safe to do with a type across threads. A type is Send if ownership of a value can be moved to another thread. A type is Sync if a shared reference &T can be used from several threads at once; precisely, T: Sync exactly when &T: Send.

Both are auto traits: the compiler implements them for a struct or enum automatically when all its fields have them. This is how Rust prevents data races at compile time. thread::spawn requires Send for the closure and everything it captures.

Rust
fn needs_send<T: Send>(_: &T) {}

Try it

Rust Checking the traits at compile time

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Why Rc cannot cross threads

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
pub unsafe auto trait Send {}
pub unsafe auto trait Sync {}

// Bounds
fn spawn_it<F: FnOnce() + Send + 'static>(f: F);
Box<dyn Fn() + Send + Sync>

// Opting in manually (unsafe) and opting out (nightly only)
unsafe impl Send for MyPtr {}
unsafe impl Sync for MyPtr {}
Trait Meaning Example
Send The value may be moved to another thread. Vec<T>, Arc<T>; MutexGuard is not
Sync &T may be shared between threads. Mutex<T>, atomics; Cell and RefCell are not
neither Confined to one thread. Rc<T>, raw pointers

Errors / panics / edge cases: the usual error is "Rc<i32> cannot be sent between threads safely" (E0277). A wrong manual unsafe impl causes undefined behavior, not a compile error.

Usage notes

  • They propagate. A struct is Send only if every field is, so one Rc or raw pointer inside turns the whole type non-Send. This is why the error often points at a field deep in a type.
  • Typical combinations. Arc<T> is Send + Sync if T: Send + Sync. Mutex<T> is Sync if T: Send. RwLock<T> is Sync if T: Send + Sync. Arc<RefCell<T>> is not Send because RefCell<T> is not Sync.
  • Send but not Sync: Cell, RefCell, mpsc::Receiver. Move them into one thread, but do not share a reference.
  • Sync but not Send: rare, for example a MutexGuard on some platforms (it must be unlocked by the thread that locked it).
  • Raw pointers are neither, so a struct containing *mut T opts out automatically. A safe wrapper that really is thread-safe can add unsafe impl Send with a comment explaining why.
  • 'static is separate. thread::spawn needs Send + 'static; scoped threads (thread::scope) need only Send since borrows are allowed.
  • Async code hits the same rule: a future held across an .await must be Send for multi-threaded executors, so avoid keeping Rc or RefCell borrows alive across awaits.
  • Not a runtime check. These traits exist only at compile time and cost nothing.