(correct me if the title is not so stated)

How to handle stdin for all strings in Rust on * nix?

Examples:

 ~$ cat file | rust_cmd # и что то делаем дальше ~$ rust_cmd < file ~$ xargs -I% rust_cmd % < file 

The code from the documentation returns only the first line.

 use std::io; fn main() -> io::Result<()> { let mut input = String::new(); io::stdin().read_line(&mut input)?; println!("You typed: {}", input.trim()); Ok(()) } 

While he began to read in this direction, please indicate the part in the documentation where it is described.

    2 answers 2

     use std::io::{stdin, BufRead}; fn main() { let stdin = stdin(); for line in stdin.lock().lines() { println!("{:?}", line); } } 

    Here the line will be of type Result<String, io::Error> , so for real use of the content as a string, you will need to "pull it out" through unwrap / expect / match /? / Etc.

    Documentation of key methods:

      I will add a variant with macros for those who came here for an answer about stdin in Rust

       use std::io; // defines a macro for reading inputs to a given buffer macro_rules! scanline { ($x:expr) => { io::stdin().read_line(&mut $x).unwrap(); }; } fn main() { let mut input = String::new(); scanline!(input); println!("{:?}",input); } 

      Links