Userinput crate

A simple cart to check the user-input data and print error for wrong input data type

eg:

[dependencies]
userinput = {git = "https://github.com/carnot-engine/userinput.git"}

sample:

fn main() 
{
    println!("Hello, world!");
    println!("Enter the char");
    let ch = userinput::char();
}
1 Like

feedback please

You could use a macro to implement the functions for all numeric types.

You could remove the recursive call in the error case and redactor the code to use a loop instead. This would remove the possibility of a user caused stack overflow.

1 Like
1 Like

I think your pattern of

match input.trim().parse::<f64>()
{
    Ok(input) => {return input;},
    Err(er) => {println!("...\nError: {}\ntry again",er);this_func()},
}

could be extracted into a helper function quite nicely. I'm imagining something like this:

fn parse_input<T: FromStr>(err_msg: &str) -> T { ... }

Right now, every one of your functions reimplements this behavior, and that's a lot of code duplication.

Besides that, and the things found by other commentors, it looks nice!

add new feature to check the input datatype
eg:
fn main()
{
let input = 20;
println!("{}",userinput::type_of(input));
}

try it.

It seems useful! I don't often need that functionality, but it seems good.

The extra printing when doing things is usually not very useful, but if it's a personal utility crate and you as the main user don't mind the extra prints, it should be fine.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.