2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-01-19 20:13:51 +00:00
async-std/examples/stdin-timeout.rs

21 lines
451 B
Rust
Raw Normal View History

2019-08-08 12:44:48 +00:00
//! Reads a line from stdin, or exits with an error if nothing is read in 5 seconds.
#![feature(async_await)]
use std::time::Duration;
2019-08-14 01:47:39 +00:00
use async_std::io;
use async_std::task;
2019-08-08 12:44:48 +00:00
fn main() -> io::Result<()> {
2019-08-15 15:59:48 +00:00
task::block_on(io::timeout(Duration::from_secs(5), async {
2019-08-08 12:44:48 +00:00
let stdin = io::stdin();
2019-08-15 15:59:48 +00:00
let mut line = String::new();
stdin.read_line(&mut line).await?;
2019-08-08 12:44:48 +00:00
2019-08-15 15:59:48 +00:00
print!("Got line: {}", line);
2019-08-08 12:44:48 +00:00
Ok(())
2019-08-15 15:59:48 +00:00
}))
2019-08-08 12:44:48 +00:00
}