Working with the Standard Input and Output
| Recipe | Crates | Categories |
|---|---|---|
| Read from the Standard Input and Write to the Standard Output or Standard Error |
Read from the Standard Input and Write to the Standard Output or Standard Error
std::io::stdin, stdout, and stderr are handles to the standard input, standard output, and standard error streams of a process, respectively. They are your primary tools for interacting with a user via the command line. stdin, by default, is connected to the keyboard; stdout and stderr are connected to the terminal or console window.
The following demonstrates how to work with them. You will rarely need to explicitly call stdout or stderr unless you need to lock them (to prevent output interleaving):
use std::io; use std::io::Write; /// Retrieve user input from the standard input: fn get_and_print_input() -> Result<String, io::Error> { println!("Please type your first name and press Enter:"); // Create a mutable `String` buffer: let mut user_input = String::new(); // Get a handle to stdin and read a line into the buffer: io::stdin().read_line(&mut user_input)?; Ok(user_input) } fn main() -> io::Result<()> { // Instead of using `print*!` to write to the standard output, // you may get an explicit handle... let stdout = io::stdout(); // ...and lock the handle if you want exclusive access: let mut handle = stdout.lock(); // Write a byte slice to the locked handle: handle.write_all(b"This is written to the standard output.\n")?; // The lock is automatically released when `handle` goes out of scope. // The same applies to the standard error of the current process: let mut stderr = io::stderr(); // We could lock `stderr` as well. stderr.write_all(b"There was an error.\n")?; // This is equivalent to `eprintln!`. Ok(()) }
Related Topics
- Reading and Writing Files.
- Paths.