-
-
Save rust-play/977d9264a63de7e5455177f0b9100902 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use futures::{future, stream}; | |
use futures::{StreamExt as _, FutureExt as _}; | |
use tokio::time::sleep; | |
use tokio::time::{Instant, Duration}; | |
async fn loopnprint(name: &str) -> Result<(), String> { | |
let now = Instant::now(); | |
loop { | |
sleep(Duration::from_millis(101)).await; | |
let elapsed = Instant::now() - now; | |
if name == "meow2" && elapsed > Duration::from_millis(1230) { | |
return Err("OHNO".to_string()); | |
} | |
println!("name: {name}, delayed: {:?}.", elapsed); | |
} | |
} | |
async fn and_loop() -> Result<(), String> { | |
let mut s = stream::FuturesUnordered::new(); | |
s.push(loopnprint("meow2")); | |
s.push(loopnprint("meow0")); | |
s.push(loopnprint("meow1")); | |
while let Some(f) = s.next().await { | |
return f; | |
} | |
Ok(()) | |
} | |
#[tokio::main] | |
async fn main() { | |
let ret = and_loop().await; | |
println!("RET: {:?}", ret); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment