Function console_utils::input

source ·
pub fn input<T>(before: &str, allow_empty: bool, new_line: bool) -> Option<T>where
    T: FromStr,
    T::Err: Debug,
Expand description

Reads user input from the console.

This function prompts the user with a message (before) and reads a line of input from the console. The input can be empty unless allow_empty is set to false. If new_line is set to true, a newline character will be printed after the prompt.

Arguments

  • before - The text to display before prompting for input.
  • allow_empty - If true, allows the input to be empty.
  • new_line - If true, adds a newline character after the prompt.

Returns

Returns an Option<T> containing the user’s input converted to the specified type, or None if the input is empty and allow_empty is true.

Example

use console_utils::input;
     
let user_input = input::<String>("Enter something: ", false, false);

match user_input {
    Some(value) => println!("You entered: {}", value),
    None => panic!("The Input cannot be None, allow_empty is false."),
}