🦀 Rust Intermediate

What are Rust's concurrency primitives (Send and Sync traits)?

Answer

Send and Sync are marker traits that encode thread-safety at the type level. A type is Send if it is safe to transfer its ownership to another thread. Almost all Rust types are Send; notable exceptions are Rc<T> (use Arc<T> instead) and raw pointers. A type is Sync if it is safe to share a reference to it across multiple threads (i.e., &T is Send). Types with interior mutability like Cell<T> and RefCell<T> are NOT Sync — use Mutex<T> instead. The compiler automatically derives Send and Sync for types whose fields are Send/Sync. These traits make Rust's thread-safety guarantees compile-time checkable — thread::spawn requires its closure to be Send.