19.4 Send and Sync

RUSTSTATUS: DEVELOPINGLEVEL: INTERMEDIATEREVIEWED: 2026-11-15

19.4.1 Motivation

A data race happens when one thread writes a value while another reads or writes it, with no synchronization between them. In C and C++ the result is undefined behavior: the program may crash or produce wrong answers, and the bug may show up only under particular timing.

Safe Rust rules out data races with two marker traits: Send and Sync. These traits make thread safety a property of types. If a type is Send, values of that type can safely move across thread boundaries. If a type is Sync, values can be safely shared across threads. The compiler checks this at compile time.

19.4.2 The claims Send and Sync make

A type is Send if it is safe to move a value to another thread. Most types are Send: an i32, a Vec<i32>, a String. Rc<T> is not, because a clone left behind in the first thread would share an unsynchronized count with the moved one. A raw pointer *const T is not Send either. The Rustonomicon calls this mostly a lint: the compiler cannot know what a raw pointer refers to, so a struct that holds one does not become Send without the author’s say-so.

A type is Sync if it is safe to share a reference &T across threads. This means multiple threads can use that &T at the same time. Most types are Sync. But Cell<T> is not Sync. A Cell<T> allows mutation through a shared reference at runtime. If two threads held &Cell<i32> to the same cell and both wrote at the same time, that would be a data race. Mutex<T> is Sync (when T is Send) because it uses locking to prevent the race.

The relationship between the two is: T is Sync if and only if &T is Send. A reference to T can go to another thread only if using it there, at the same time as the original thread, is safe.

The compiler implements Send and Sync automatically for structs and enums based on their fields; the Rust Reference calls them auto traits. A struct is Send if all of its fields are Send. A struct is Sync if all of its fields are Sync. This happens at the type level, with no runtime cost.

struct MyValue {
    x: i32,      // Send and Sync
    s: String,   // Send and Sync
}
// MyValue is Send and Sync

struct MyPointer {
    p: *const i32,  // not Send
}
// MyPointer is not Send

If a field is not Send, the struct is not Send. The rule is conservative: the struct still compiles, but the compiler refuses to move a value of it to another thread.

Occasionally a developer needs to override the automatic implementation. A type holds a raw pointer to memory it owns alone, such as a buffer from a C allocator. The compiler says it is not Send, but the developer knows the pointer is safe to move. The developer can assert Send manually with unsafe impl, signaling that they are claiming something the compiler cannot verify. This is rare and requires documentation.

19.4.3 Errors Send and Sync prevent

The Send and Sync traits allow the compiler to reject programs that have data races. Here is an example:

let x = RefCell::new(42);
std::thread::scope(|s| {
    // error[E0277]: `RefCell<i32>` cannot be shared between threads safely
    // the trait `Sync` is not implemented for `RefCell<i32>`
    s.spawn(|| { *x.borrow_mut() += 1; });
    s.spawn(|| { *x.borrow_mut() += 1; });
});

RefCell<T> is Sync for no T at all, because its borrow flag is an ordinary integer that two threads could update at once. Both read the flag as zero (no borrows), both write minus one (the value that marks a mutable borrow), and both then believe they hold the only mutable borrow. That is the same lost update drawn on race conditions, with a borrow flag in place of a counter. It is also what the compiler rejects here: both closures capture &x, and sharing a reference across threads is exactly what Sync governs.

Note which trait did the work. RefCell<T> is Send whenever T is, so moving sole ownership of one into a single thread compiles without complaint. Handing the same cell to two threads does not. Sending and sharing are different questions, and RefCell answers them differently.

This check is static. Safe Rust that compiles has no data races, as long as the unsafe code beneath it, in the standard library and elsewhere, is correct.

Here is another example:

let x = Rc::new(42);
std::thread::spawn(move || {
    // error[E0277]: `Rc<i32>` cannot be sent between threads safely
    x.clone();
});

Rc uses non-atomic reference counting. If two threads decremented the count at the same time, the result would be a data race, which is undefined behavior. The compiler forbids sending Rc to another thread. To share data across threads, use Arc instead.

19.4.4 Implementing Send and Sync manually

Most types get Send and Sync automatically. But for types with raw pointers, the developer must implement them manually when they are wanted.

pub struct StableAddress<T> {
    ptr: *const T,
}

unsafe impl<T> Send for StableAddress<T> where T: Send {}
unsafe impl<T> Sync for StableAddress<T> where T: Sync {}

The StableAddress type wraps a raw pointer. Raw pointers are not Send or Sync by default, so the developer asserts the two impls by hand. They are correct only if StableAddress behaves like the safe type it stands in for. If it owns the T it points to, as a Box<T> does, then Send where T: Send matches Box. If it is a shared view, like &T, then Send must require T: Sync, because &T is Send only when T is Sync; with the bound written as T: Send, a StableAddress<Cell<i32>> could carry shared access to one Cell into a second thread. Whether the object it points to ever moves in memory has no bearing on either impl.

This is rare in practice. Most code never implements these traits manually. The automatic implementation is correct for almost all types.

19.4.5 Other systems

Most languages have no static check for thread safety. CPython’s default build uses the Global Interpreter Lock so that only one thread runs Python bytecode at a time; the free-threaded build added in Python 3.13 removes it. Java allows shared mutable state and relies on synchronization keywords like synchronized. The language does not prove that synchronization is correct.

Go has no Send or Sync traits. It encourages goroutines to pass values through channels instead of sharing them, but they can still share memory, and the compiler does not stop them from racing on it.

Rust gives the compiler the information it needs. Send and Sync are part of the type, and the compiler checks them at every thread boundary: everything that crosses must be Send, and everything shared must be Sync. The check happens once, at compile time, with no run-time cost. An unsafe impl can still make a false claim, and the compiler takes it on trust.

19.4.6 Relevance to my work

In Rust bindings to GNU R, such as the libR-sys crate, a SEXP is a raw pointer, *mut SEXPREC, so it is not Send, which is what GNU R needs. The SEXP graph embeds bookkeeping for garbage collection: mark bits, reference counts and generation information that change as R runs. The collector can run at any allocation on R’s main thread. If another thread reads or allocates while it runs, both threads touch the same object headers, which is a data race.

In GNU R, the heap and the collector are process-global, and no lock protects them. “Writing R Extensions” calls any use of the R API from threaded code “for experts only” and says R errors must only happen on the main thread. GNU R is C code, so nothing at the type level enforces this.

In practice, Send and Sync turn a design choice into something the compiler checks. If a type is not Send, it cannot be moved to another thread. If a type is not Sync, a reference to it cannot be shared across threads. An interpreter that owns its heap is per-thread; an interpreter wrapped in Arc<Mutex<_>> is shared.

rrs makes Interp, the struct that owns one interpreter’s heap, collector, symbol table and string cache, Send and deliberately not Sync, and a compile_fail doc test checks the second half. An interpreter can move to another thread, but only one thread can use it at a time, so its heap needs no lock. A handle into that heap is a Handle, a newtype over a u32, and it is Send, so the type system does not stop a handle from being used against the wrong interpreter. A handle past the end of the heap hits Rust’s bounds check, and the debug verifier, which the tests run, catches one that points at a freed slot; one that lands on a live slot in the other heap goes unnoticed.

Wrapping a GNU R SEXP in Arc<Mutex<_>> does not make it shareable. Mutex<T> is Sync only when T is Send, and a raw pointer is not, so the compiler still rejects it with E0277. A lock would not help anyway, because R’s collector writes to object headers without taking it.

19.4.7 Failure modes

A mistake is to claim Send for a type that is not safe to send. This requires unsafe impl, which is a red flag. If the claim is wrong, the program has undefined behavior.

struct BadPointer(*const Cell<i32>);
unsafe impl Send for BadPointer {}
// WRONG: the receiving thread can write the Cell while the sender
// still uses it, and Cell is not Sync

Another mistake is to assume that a type is Send when it is not. If a struct holds a raw pointer, the compiler treats it as not Send, and moving it to another thread fails with E0277 until the developer asserts unsafe impl Send.

Further reading

Where this appears

  • r-in-rust (rrs) An rrs interpreter is Send and not Sync, so one thread at a time owns it and its heap needs no lock.

Where to go next

Back to top