File: stdin-echo.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 (28 lines) | stat: -rw-r--r-- 686 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
21
22
23
24
25
26
27
28
//! Echoes lines read on stdin to stdout.

use async_std::io;
use async_std::prelude::*;
use async_std::task;

fn main() -> io::Result<()> {
    task::block_on(async {
        let stdin = io::stdin();
        let mut stdout = io::stdout();
        let mut line = String::new();

        loop {
            // Read a line from stdin.
            let n = stdin.read_line(&mut line).await?;

            // If this is the end of stdin, return.
            if n == 0 {
                return Ok(());
            }

            // Write the line to stdout.
            stdout.write_all(line.as_bytes()).await?;
            stdout.flush().await?;
            line.clear();
        }
    })
}