async fn coroutine() {Tsh华陈数据科技 let mut n = 0;Tsh华陈数据科技 while n < 100 {Tsh华陈数据科技 n += 1;Tsh华陈数据科技 println!("coroutine:{}", n);Tsh华陈数据科技 sleep(Duration::from_secs_f64(0.3));Tsh华陈数据科技 }Tsh华陈数据科技 } Tsh华陈数据科技
use futures;Tsh华陈数据科技 use std::thread::sleep;Tsh华陈数据科技 use std::time::Duration;Tsh华陈数据科技 Tsh华陈数据科技 async fn coroutine() {Tsh华陈数据科技 let mut n = 0;Tsh华陈数据科技 while n < 100 {Tsh华陈数据科技 n += 1;Tsh华陈数据科技 println!("coroutine:{}", n);Tsh华陈数据科技 sleep(Duration::from_secs_f64(0.3));Tsh华陈数据科技 }Tsh华陈数据科技 }Tsh华陈数据科技 Tsh华陈数据科技 fn main() {Tsh华陈数据科技 let coroutine = coroutine();Tsh华陈数据科技 Tsh华陈数据科技 futures::executor::block_on(coroutine); Tsh华陈数据科技 }
use futures;Tsh华陈数据科技 use std::thread::sleep;Tsh华陈数据科技 use std::time::Duration;Tsh华陈数据科技 Tsh华陈数据科技 async fn coroutine1() {Tsh华陈数据科技 let c = coroutine2();Tsh华陈数据科技 c.await;Tsh华陈数据科技 Tsh华陈数据科技 let mut n = 0;Tsh华陈数据科技 while n < 100 {Tsh华陈数据科技 n += 1;Tsh华陈数据科技 println!("coroutine1:{}", n);Tsh华陈数据科技 sleep(Duration::from_secs_f64(0.3));Tsh华陈数据科技 }Tsh华陈数据科技 }Tsh华陈数据科技 Tsh华陈数据科技 async fn coroutine2() {Tsh华陈数据科技 let mut n = 0;Tsh华陈数据科技 while n < 100 {Tsh华陈数据科技 n += 1;Tsh华陈数据科技 println!("coroutine2:{}", n);Tsh华陈数据科技 sleep(Duration::from_secs_f64(0.3));Tsh华陈数据科技 }Tsh华陈数据科技 }Tsh华陈数据科技 Tsh华陈数据科技 fn main() {Tsh华陈数据科技 let coroutine = coroutine1();Tsh华陈数据科技 Tsh华陈数据科技 println!("coroutine created");Tsh华陈数据科技 sleep(Duration::from_secs(5));Tsh华陈数据科技 println!("block_on coroutine");Tsh华陈数据科技 Tsh华陈数据科技 futures::executor::block_on(coroutine); Tsh华陈数据科技 }