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-echo.rs

29 lines
686 B
Rust

5 years ago
//! Echoes lines read on stdin to stdout.
5 years ago
use async_std::io;
use async_std::prelude::*;
5 years ago
use async_std::task;
5 years ago
fn main() -> io::Result<()> {
task::block_on(async {
let stdin = io::stdin();
let mut stdout = io::stdout();
5 years ago
let mut line = String::new();
loop {
// Read a line from stdin.
let n = stdin.read_line(&mut line).await?;
5 years ago
// 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?;
5 years ago
line.clear();
}
})
}