Skip to content

Instantly share code, notes, and snippets.

@Coutlaw
Created May 27, 2020 11:00
Show Gist options
  • Save Coutlaw/cc83197f6f37bb7113aa7c15e1e443ef to your computer and use it in GitHub Desktop.
Save Coutlaw/cc83197f6f37bb7113aa7c15e1e443ef to your computer and use it in GitHub Desktop.
Thread Spawn in rust
use std::thread;
use std::time::Duration;
fn main() {
// this thread will die when the main thread has ended
// but this spawned thread will start printing outputs while the main thread continues on
thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
}
});
for i in 1..5 {
println!("hi number {} from the main thread!", i);
thread::sleep(Duration::from_millis(1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment