Skip to main content

Fearless concurrency with threads

Rust calls this "fearless concurrency" because the same ownership rules that manage memory also prevent data races. If two threads could touch the same value unsafely, the program does not compile.

`std::thread::spawn` starts a real operating-system thread. The `move` keyword hands ownership of captured values to the new thread, which is exactly what the compiler needs to prove nothing is shared by accident.

For values that genuinely must be shared, `Arc<Mutex<T>>` gives you reference counting plus a lock — and the type system will not let you read the inner value without taking the lock first.

Run it and change it

The editor below is live: edit anything and the real compiler output updates by itself.

Your Rust