Rust的多线程程序

一定要用闭包?

感觉有点过了。

use std::thread;
use std::time::Duration;

fn main() {

    let v = vec![1, 2, 3];
    let handle = thread::spawn(move || {
        println!("Here's a vector: {:?}", v);
        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));
    }

    handle.join().unwrap();

}

猜你喜欢

转载自www.cnblogs.com/aguncn/p/11441533.html