Press ENTER to continue

ENTER STDIN

A small function I wanted to use in one of my other examples so I can "hold" the process while switching to another terminal to look at the memory usage via htop. I ended up using a crate to show the used and free memory from inside the example.

examples/press-enter-to-continue/src/main.rs

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

fn main() {
    println!("Before");
    wait_for_enter().unwrap();
    println!("After");
}

fn wait_for_enter() -> Result<(), std::io::Error> {
    print!("Press ENTER to continue!");
    std::io::stdout().flush()?;

    let mut buffer = String::new();
    let stdin = io::stdin();
    stdin.read_line(&mut buffer)?;

    Ok(())
}

See also the prompt as this is just a simple verion of it.

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo