A simple tool that asks for data until the data is valid.
If you run into any issues or need help with using read_input
in your project please email incoming+efunb/read_input@incoming.gitlab.com
When writing simple programs you will often need to take input from the user. If the user inputs invalid information the program needs to try asking them again. Having to make this loop distracts from the useful logic in your program.
Read input attempts to make it easy to get input from the user without having to think about converting types.
Add
toml
read_input = "0.0.2"
to your cargo.toml
under [dependencies]
and add
```rust
extern crate read_input;
use read_input::*; ``` to your main file.
You can get input with.
rust
let input = Type::simple_input();
Where Type
is the type you want. Currently the types you can use include i8
, u8
, i16
, u16
, f32
, i32
, u32
, f64
, i64
, u64
, i128
, u128
and String
.
So if you want a valid unsigned 32bit value you could write.
rust
let input = u32::simple_input();
You can also add your own checks to ensure the value meets your criteria. If you want a signed 16bit value between 4 and 9 you could write.
rust
let input = i16::valid_input(|x| 4 < *x && *x < 9);
The read_input
associated function allows for custom error messages and validation at the same time.
If you are viewing this from GitHub then this is a read only copy. Please contribute to the GitLab copy here.