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