Expand description
Simplify getting user input for your CLI applications
Check out the examples, too:
cargo run --example closing
demonstrates quitting the program earlycargo run --example data
demonstrates getting data from the usercargo run --example hello
is a basic hello world program
Example usage:
let name: String = Input::new()
.prompt("Enter your name: ")
.wait();
println!("Hello, {}!", name);
You can also set error messages for when the user messes up the input.
let number: i32 = Input::new()
.err_msg("That wasn't a number; please try again")
.prompt("Enter a number: ")
.wait();
println!("Your number is: {}", number);
You can choose to just get the first input, regardless of whether it’s good.
let number: Option<i32> = Input::new()
.prompt("Enter a number: ")
.read();
match number {
Some(n) => println!("Your number is: {}"),
None => println!("You didn't enter a number!")
}
You can specify a keyword that will end the program when entered
let number: i32 = Input::new()
.quit("quit") // this can result in the program ending early
.prompt("Enter a number: ")
.wait();
println!("Your number is: {}", number);
You can re-use the same input object for multiple inputs.
let mut input = Input::new()
.err_msg("Unexpected input; please retry")
.quit("quit");
let name: String = input.prompt("Enter your name: ").wait();
let age: u32 = input.prompt("Enter your age: ").wait();
let weight: f64 = input.prompt("Enter your weight: ").wait();
println!("Name: {}\nAge: {}\nWeight: {}", name, age, weight);
Structs§
- Input
- Handler for easily getting user input from the command line