2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-01-16 10:49:55 +00:00
async-std/examples/stdin-timeout.rs

21 lines
521 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.
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 17:26:10 +00:00
// This async scope times out after 5 seconds.
2019-08-15 15:59:48 +00:00
task::block_on(io::timeout(Duration::from_secs(5), async {
let stdin = io::stdin();
2019-08-08 12:44:48 +00:00
2019-08-15 17:26:10 +00:00
// Read a line from the standard input and display it.
2019-08-15 15:59:48 +00:00
let mut line = String::new();
stdin.read_line(&mut line).await?;
2019-08-15 17:26:10 +00:00
dbg!(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
}