I just had a similar issue, so here’s a simple mod file that will get take a simple string prompt and get a single line of input from the user. Please keep in mind that anything from a user could be hijacked by other processes so you need to run validations on what you got if this input will ever end up in a system with sensitive data.
Also if someone thinks this is useful enough to put on crates.io I’m fine with that, just mention me (Xpyder), and remove the .trim()
. I’d do it myself but I haven’t gotten that far in the Rust Book yet and I don’t know it’s use case is wide enough to warrant publishing anyways.
use simple_user_input::get_input;
fn main(){
let input: String = get_input("Please type something...");
println!("{}",input);
}
mod simple_user_input {
use std::io;
pub fn get_input(prompt: &str) -> String{
println!("{}",prompt);
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(_goes_into_input_above) => {},
Err(_no_updates_is_fine) => {},
}
input.trim().to_string()
}
}