Rust Send and Sync
Send and Sync are std::marker traits to support concurrency.
Send
Send is a marker indicating the data can be transferred across thread. Almost all Rust types are Send, but there are exceptions like Rc<T>.
Sync
Sync is a marker indicating that the data can be safely referenced from another thread. In other words, any type T is Sync if &T (an immutable reference to T) is Send. Primitive types are Sync, and any Rust types composed of only Sync types are Sync.
Sync is the most similar concept in Rust to the colloquial meaning of the phrase “thread-safe”.
Common Types
Rcis neitherSendnorSync. Sending anRcacross thread means multiple threads can access to the reference counter non-atomically- The
RefCell<T>andCell<T>types areSend(ifT: Send), but they are notSync. ARefCellcan be sent across a thread boundary, but not accessed concurrently because the implementation of borrow checking thatRefCell<T>does at runtime is not thread-safe. Mutex<T>isSendandSync- The type
MutexGuard<'a, T>that is returned byMutex::lockisSync(ifT: Sync) but notSend. It is specifically notSendbecause some platforms mandate that mutexes are unlocked by the same thread that locked them.