On this page
  1. Sync
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

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

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