Crate prompted

Source
Expand description

This crate provides macros for easy non-newline-terminated flushed printing, and for input line reading. These macros are intended for new Rust users and for folks who need no more for simple applications.

§Example

Here’s an example adapted from the “Guessing Game” example in The Rust Programming Language.

use std::cmp::Ordering;
use prompted::input;

fn main() {
    println!("Guess the number!");

    let n = 100;
    let secret_number = 37;

    loop {
        let guess = input!("Please input your guess (1-{}): ", n);

        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        println!("You guessed: {}", guess);

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }
    }
}

Macros§

eprompt
Same functionality as prompt!() except using stderr instead of stdout.
input
If a format!() describing a prompt is present, print it on stdout and then flush. Then read a line from stdin and return it after removing the line ending.
prompt
Same functionality as print!() except that stdout is flushed at the end.

Functions§

eflush
Flush standard error. Intended primarily for use by macros.
flush
Flush standard output. Intended primarily for use by macros.
read_line
Read a line from standard input. Removes a trailing newline if present. Intended primarily for use by macros.