什么相当于Go的射程时间。快点?

时间:2015-04-16 09:44:37

标签: concurrency rust

我刚开始学习Rust,来自Go。如何实现长并发轮询?

// StartGettingWeather initialize weather getter and setter
func StartGettingWeather() {

    // start looping
    for i := range time.Tick(time.Second * time.Duration(delay)) {
        _ = i
        loopCounter++
        fmt.Println(time.Now().Format(time.RFC850), " counter: ", loopCounter)
        mainWeatherGetter()
    }
}

我会将此func称为go StartGettingWeather()

1 个答案:

答案 0 :(得分:2)

Rust线程是OS线程,它们使用OS调度程序,因此您可以使用thread::sleep_ms来模拟它:

use std::thread;

fn start_getting_weather() {
    let mut loop_counter = 0;
    loop {
        loop_counter += 1;
        println!("counter: {}", loop_counter);
        main_weather_getter();
        thread::sleep_ms(delay);
    }
}

thread::spawn(move || start_getting_weather());