🦀 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.
More Rust Questions
View all →- Intermediate How does error handling with the ? operator work in Rust?
- Intermediate What are the thiserror and anyhow crates for error handling?
- Intermediate What are trait objects (dyn Trait) in Rust?
- Intermediate What are generics in Rust and how do where clauses work?
- Intermediate What are Rust smart pointers?