liveBook · Manning

Sync

As explained in Code Like a Pro in Rust

Sync is a Rust marker trait indicating that references to a value can be shared safely between threads.

Sync is Rust’s marker trait for values that can be safely shared through references across threads. The compiler derives it automatically when appropriate; application code typically combines Arc, Mutex, or RwLock rather than implementing it directly. section 6.5

Use the trait distinction to interpret compiler errors and choose synchronization primitives:

  • Send covers moving ownership between threads; Sync covers sharing references to the same value. section 6.5
  • Use Arc when shared ownership must cross threads, then add AtomicI32, Mutex, or RwLock according to the mutation pattern. section 6.5

Sharing an atomic counter

A lazily initialized static AtomicI32 can be shared safely, and each test can update it with fetch_add and explicit ordering.

How Code Like a Pro in Rust covers it

How deep it goes
Defines Send and Sync, shows compiler errors, and applies Arc, atomics, lazy initialization, and locking without trait internals.
Examples
Demonstrates atomic counters, lazy static initialization, compiler diagnostics, and Mutex-guarded parallel tests.
What it assumes
Assumes Rust familiarity and introduces Send, Sync, ownership errors, and shared-state synchronization in context.
What you can do after
Diagnose thread-sharing errors and select Arc, atomics, Mutex, or RwLock for shared state.
How it connects
Connects Sync with Send, Arc, Mutex, RwLock, atomic types, ownership, and shared mutable state.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.