Send and Sync
Quick answer
Send describes whether ownership can safely cross thread boundaries. Sync means that shared references to a type can safely cross thread boundaries (T: Sync exactly when &T: Send).
Check the bounds of shared data
These are marker auto traits, normally determined from a type's components. Rc uses non-atomic reference counting and is neither Send nor Sync; Arc<T> supplies atomic reference counts but does not make arbitrary T thread-safe. Shared mutable data generally needs synchronization. Unsafe manual implementations must uphold the traits' safety contracts.
use std::sync::{Arc, Mutex};fn check<T: Send + Sync>() {}fn main() { check::<Arc<Mutex<Vec<u32>>>>(); println!("bounds satisfied");}Expected stdout (not executed):
bounds satisfiedThe generic helper documents the required sharing contract.
use std::rc::Rc;fn main() { let data = Rc::new(7); std::thread::spawn(move || println!("{}", *data)).join().unwrap();}Expected: compilation fails; exact diagnostic not verified.
Use ownership transfer, or Arc when shared ownership is required and its inner type has suitable bounds.
Common mistake
Do not add unsafe impl Send merely to silence a compiler error.
Try it yourself
Replace Rc with Arc in the rejected example and explain why the integer satisfies the bounds.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Send and Sync — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
Does wrapping Rc<T> in Arc automatically make it safe to share across threads?
Send and Sync — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::sync::{Arc, Mutex}; fn check<T: Send + Sync>() {} fn main() { check::<Arc<Mutex<Vec<u32>>>>(); println!("bounds satisfied"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Send and Sync — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::rc::Rc; fn main() { let data = Rc::new(7); std::thread::spawn(move || println!("{}", *data)).join().unwrap(); } ``` Which outcome or interpretation best matches this example?