On this page
Sync
Prev: Send | Up: builtins | Next: match
Rel: builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs — Sync
Tags: #public #prog #rust #builtins #tier/standard
Availability
Built-in / Prelude (Auto Marker Trait) — available with zero use imports.
Syntax & Overview
unsafe auto trait Sync {}
Marks types safe to share immutable references (&T) across multiple threads simultaneously (T: Sync iff &T: Send).
Notes & Pitfalls
T: Synciff&T: Send: Means multiple threads can safely hold immutable&Treferences simultaneously.- Interior mutability without locks is
!Sync:Cell<T>andRefCell<T>are notSync; wrap shared mutable state inMutex<T>,RwLock<T>, or atomics.
Example
use std::sync::Arc;
use std::thread;
fn main() {
let shared = Arc::new(vec![10, 20, 30]); // Vec<i32> is Sync -> Arc<Vec<i32>> is Send
let clone = Arc::clone(&shared);
let total = thread::spawn(move || clone.iter().sum::<i32>()).join().unwrap();
assert_eq!(total, 60);
}
Linked from
Related notes
- tuts/rust/builtins/match Linked here; links to this page; shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/Send Linked here; links to this page; shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins Linked here; links to this page; shared tags: #builtins, #prog, #rust
- tuts/rust/ownership_and_internals/ownership_borrowing_and_lifetimes Linked here; shared tags: #prog, #rust
- tuts/rust/builtins/Clone shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/Copy shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/Drop shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/From shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/Iterator shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/macro_rules shared tags: #builtins, #prog, #rust, #tier