线程
Rust 通过 spawn
函数提供了一种生成原生操作系统线程的机制,该函数的参数是一个移动闭包。
use std::thread; const NTHREADS: u32 = 10; // This is the `main` thread fn main() { // Make a vector to hold the children which are spawned. let mut children = vec![]; for i in 0..NTHREADS { // Spin up another thread children.push(thread::spawn(move || { println!("this is thread number {}", i); })); } for child in children { // Wait for the thread to finish. Returns a result. let _ = child.join(); } }
这些线程将由操作系统调度。