Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rust - flush STDOUT - read STDIN

  • print!

  • Write

  • std::io::Write

  • We use print! and not println!

  • We use the std::io::Write trait that includes the flush method.

use std::io;
use std::io::Write;

fn main() {
    let mut name = String::new();

    print!("Please type in your name: ");
    io::stdout().flush().expect("Oups");
    io::stdin()
        .read_line(&mut name)
        .expect("Failed to get input");
    name = name.trim_end().to_owned();

    println!("Hello {name}, how are you?");
}