Rust Fundamentals interview question · Most asked
How does Rust prevent data races in concurrent code?
Summary
The Send and Sync marker traits are checked by the compiler: only Send types can move to other threads and only Sync types can be shared. Shared mutable state needs synchronisation like Arc<Mutex<T>>, so unsynchronised sharing doesn't compile.
Longer Answer
Channels transfer ownership between threads; scoped threads can borrow local data safely; async runtimes like Tokio handle I/O concurrency.
Key Points
- Send/Sync checked at compile time.
- Arc for shared ownership, Mutex/RwLock for mutation.
- Channels and scoped threads.
Deep dive: Rust Fundamentals: Fearless Concurrency