You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
async-std/examples/stdin-timeout.rs

29 lines
666 B
Rust

//! Reads a line from stdin, or exits with an error if nothing is read in 5 seconds.
#![feature(async_await)]
use std::time::Duration;
use async_std::{io, prelude::*, task};
fn main() -> io::Result<()> {
task::block_on(async {
let stdin = io::stdin();
let mut line = String::new();
match stdin
.read_line(&mut line)
.timeout(Duration::from_secs(5))
.await
{
Ok(res) => {
res?;
print!("Got line: {}", line);
}
Err(_) => println!("You have only 5 seconds to enter a line. Try again :)"),
}
Ok(())
})
}