std::io::stdin() / stdout()
Rust handles standard I/O through std::io::stdin() and std::io::stdout(). You can read keyboard input line by line with read_line(), and write output using the println!() or write!() macros.
Syntax
use std::io::{self, BufRead, Write};
// Read one line from standard input.
let mut input = String::new();
io::stdin().read_line(&mut input)?;
let trimmed = input.trim(); // Remove the trailing newline.
// Lock stdin for efficiency when reading multiple lines.
let stdin = io::stdin();
for line in stdin.lock().lines() {
let line = line?;
println!("{}", line);
}
// Write to standard output.
print!("Enter your input: ");
io::stdout().flush()?; // Flush the buffer immediately.
// Write to standard error.
eprintln!("Error: {}", msg);
writeln!(io::stderr(), "Error details: {}")?;
Method List
| Method / Function | Description |
|---|---|
| io::stdin() | Returns the standard input handle (a Stdin struct). |
| stdin.read_line(&mut s) | Reads one line (including the newline character) into a String. Returns Result<usize>. |
| stdin.lock().lines() | Locks stdin and returns a line iterator. More efficient when reading multiple lines. |
| io::stdout() | Returns the standard output handle (a Stdout struct). |
| stdout.flush() | Flushes the output buffer immediately. Required after output without a trailing newline. |
| io::stderr() | Returns the standard error handle (a Stderr struct). |
| input.trim() | Removes leading and trailing whitespace and newlines. Always call this after read_line. |
| s.parse::<T>() | Parses a string into another type, such as a number. |
Sample Code
use std::io::{self, Write};
fn main() {
// --- Basic input ---
print!("Enter your name: ");
io::stdout().flush().unwrap(); // Display the prompt immediately.
let mut name = String::new();
io::stdin().read_line(&mut name).unwrap();
let name = name.trim(); // Remove the trailing newline.
println!("Hello, {}!", name);
// --- Numeric input ---
print!("Enter your age: ");
io::stdout().flush().unwrap();
let mut age_str = String::new();
io::stdin().read_line(&mut age_str).unwrap();
let age: u32 = age_str.trim().parse().unwrap_or(0);
println!("Next year you will be {}", age + 1);
// --- Input with error handling ---
let num = loop {
print!("Enter an integer from 1 to 10: ");
io::stdout().flush().unwrap();
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
match buf.trim().parse::<u32>() {
Ok(n) if n >= 1 && n <= 10 => break n,
Ok(_) => eprintln!("Please enter a number between 1 and 10"),
Err(_) => eprintln!("Please enter a valid integer"),
}
};
println!("You entered: {}", num);
}
Notes
read_line() appends the trailing newline character ('\n' or '\r\n') to the string. Always call trim() afterward to remove leading and trailing whitespace and newlines before using the value.
To display a prompt immediately after using print!() (which does not add a newline), you need to call io::stdout().flush(). Rust's standard output is buffered, so without a newline the output may remain in the buffer.
For file reading and writing, see File::open() / File::create(). For details on formatted output, see println!() / print!() / format!().
If you find any errors or copyright issues, please contact us.