File: stdin-timeout.rs

package info (click to toggle)
rust-async-std 1.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,992 kB
  • sloc: sh: 13; makefile: 8
file content (20 lines) | stat: -rw-r--r-- 521 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//! Reads a line from stdin, or exits with an error if nothing is read in 5 seconds.

use std::time::Duration;

use async_std::io;
use async_std::task;

fn main() -> io::Result<()> {
    // This async scope times out after 5 seconds.
    task::block_on(io::timeout(Duration::from_secs(5), async {
        let stdin = io::stdin();

        // Read a line from the standard input and display it.
        let mut line = String::new();
        stdin.read_line(&mut line).await?;
        dbg!(line);

        Ok(())
    }))
}