Concurrency primitives in Rust are the fundamental building blocks that allow multiple parts of a program to run simultaneously — safely and without data races. They are the low-level tools you use to coordinate concurrent tasks, share data between threads, and synchronize execution.

Why Rust Is Special Here

Rust’s ownership and type system enforce concurrency safety at compile time, not at runtime. Many bugs that would silently corrupt data in other languages become compile errors in Rust, which is why Rust calls its approach “fearless concurrency”. doc.rust-lang

The Core Primitives

  • Threads — the most basic primitive; independent paths of execution that run concurrently, letting you exploit multi-core processors earthly
  • Channels (mpsc) — typed message-passing pipes with a sender and receiver handle; one thread sends data, another receives it, avoiding shared memory entirely news.ycombinator
  • Mutex (Mutex<T>) — short for mutual exclusion; only one thread can access the protected data at a time, preventing data races on shared state earthly
  • Arc (Arc<T>)Atomic Reference Counting; lets multiple threads share ownership of a value safely doc.rust-lang
  • RwLock<T> — like a Mutex, but allows many simultaneous readers or one exclusive writer
  • Atomic types — low-level primitives (e.g., AtomicUsize) for lock-free, thread-safe operations on simple values web.mit

Key Traits: Send and Sync

Rust enforces concurrency rules through two marker traits: web.mit

  • Send — a type can be transferred (moved) to another thread
  • Sync — a type can be safely referenced from multiple threads simultaneously

These traits are automatically implemented by the compiler where safe, and withheld where they aren’t — so you can’t accidentally send a non-thread-safe type across a thread boundary. google.github

A Simple Mental Model

Think of concurrency primitives as traffic rules for threads. Channels say “pass the data by handing it off”, while Mutex/Arc say “share the data, but take turns”. Rust’s compiler acts as the traffic enforcer, rejecting unsafe patterns before your code ever runs. dzone