pub fn read_input<T>(prompt: Option<&str>) -> Result<T, String>
Expand description
Reads input from the console, optionally displaying a prompt message.
This function can:
- Display a custom prompt message
- Read input until the user presses Enter
- Parse the input into any type that implements FromStr
- Act as a pause mechanism when no prompt is provided
§Type Parameters
T
: The type to parse the input into. Must implement FromStr and Default.
§Arguments
prompt
: An optional prompt message to display before reading input.
§Returns
T
: The parsed input valueString
: The raw input string if parsing fails or no parsing is needed
§Examples
let number: i32 = read_input(Some("Enter a number: ")).unwrap();
let name: String = read_input(Some("Enter your name: ")).unwrap();
read_input::<String>(None); // Acts as a pause