std::thread for OS threads.
Spawn thread:
1let handle = std::thread::spawn(|| {2 for i in 1..10 {3 println!("Spawned: {}", i);4 std::thread::sleep(std::time::Duration::from_millis(1));5 }6});78for i in 1..5 {9 println!("Main: {}", i);10 std::thread::sleep(std::time::Duration::from_millis(1));11}1213handle.join().unwrap();
Move closure:
1let name = String::from("Alice");2let handle = std::thread::spawn(move || {3 println!("Hello, {}!", name);4});5// name is now moved
Key: spawn for threads, join to wait.