Systems Design Questions · 18 min · free lesson

Send and Sync

Two marker traits decide what may cross a thread boundary.

Core idea

T is Sync exactly when &T is Send.

Send allows moving a value to another thread; Sync allows shared references across threads.
Send and Sync express different thread-safety promises.

Understand it

Send permits ownership transfer across threads. Sync means that a shared reference can be sent safely; formally, T is Sync when &T is Send. Auto-trait implementations depend on the fields and inner types, so Arc alone cannot make an arbitrary payload thread-safe.

Send allows ownership of a value to cross a thread boundary. Sync allows shared references to the value to be used from multiple threads; formally, &T is Send when T is Sync. These are marker traits, usually inferred from the fields of a type. A type may satisfy one but not the other. The distinction is about transfer versus shared access, so answer those as separate questions rather than saying a type is simply 'thread safe'.

Rc<T> is not suitable across threads because its reference count is non-atomic. Arc<T> uses an atomic reference count, so it can share ownership across threads when the contained T meets the required bounds. That does not make arbitrary mutation of T safe. If workers need to update one value, combine Arc with a mutex or use a narrower atomic operation. If they only read immutable data, Arc alone may suffice.

When the compiler rejects a value captured by thread::spawn, read whether the closure needs Send, whether a shared reference also needs Sync, and whether the captured value is owned or borrowed. Try moving a plain owned String first: transfer is fine. Then compare Rc<String> and Arc<String>. Do not add unsafe impl Send or Sync merely to satisfy a bound; those declarations promise safety to code you cannot inspect. The interview-quality answer gives both the trait definition and the design consequence.

Interview follow-up: Is Arc<RefCell<u32>> suitable for shared mutation across threads? Identify the missing Sync guarantee and a suitable alternative.

Read the code

fn run<F>(job: F)
where
    F: FnOnce() + Send + 'static,
{
    std::thread::spawn(job);
}

The bound is exactly what thread::spawn asks for.

Practice starter

fn main() {
    let shared = std::rc::Rc::new(1);
    std::thread::spawn(move || println!("{shared}"));
}

Practise this code in Playground ↗

Try it

Explain why Rc cannot cross threads and Arc can.

What success looks like: You name the atomic reference count as the deciding detail.

Check your understanding

Why does moving this Rc into thread::spawn fail its Send bound?

Reveal the answer
  1. Rc cannot transfer shared ownership across threads with its non-atomic count.
  2. The move closure borrows shared instead of taking ownership of it.
  3. Every thread closure must borrow its captures through a shared reference.

Arc<T> is Send and Sync only when the relevant T: Send + Sync bounds hold. Atomic reference counting alone does not make T thread-safe.

Go deeper

Common misconception: The move closure borrows shared instead of taking ownership of it.

move does take ownership of shared. The closure fails anyway because it now contains an Rc, and Rc is not Send, so ownership alone does not satisfy the bound.

  1. Identify the contract: Two marker traits decide what may cross a thread boundary.
  2. Trace the concrete example: The bound is exactly what thread::spawn asks for.
  3. Test the changed constraint: Is Arc<RefCell<u32>> suitable for shared mutation across threads? Identify the missing Sync guarantee and a suitable alternative.

Transfer challenge: Is Arc<RefCell<u32>> suitable for shared mutation across threads? Identify the missing Sync guarantee and a suitable alternative.

Connections

  • Option vs Result: Absence, failure, and propagation
  • Box, Rc, and Arc: Ownership and allocation choices
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Send and Sync (courseId "interview", lessonId "q-send-sync")
Link: https://crabrix.com/learn/interview/q-send-sync/
Goal: T is Sync exactly when &T is Send.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-send-sync) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.