trait

Sync: safely share references

Sync expresses that shared references to a type can be transferred across threads safely. It does not mean arbitrary unsynchronized mutation is permitted. Cell and RefCell are not Sync; immutable ordinary strings are Sync. Expected stdout (not executed): 4 Common mistake: Assuming shared ownership automatically makes interior mutation safe.

Formal Syntax & Structure

rust syntaxrust
T: Sync iff &T: Send

Working Example

Example Snippetrust
fn main() {    fn require_sync<T: Sync>(_: &T) {}    let text = String::from("book");    require_sync(&text);    std::thread::scope(|s| { let h = s.spawn(|| text.len()); println!("{}", h.join().unwrap()); });}
Rust Reference