I've recently started learning Rust, and I've managed to whip up this function, which reads and parses a thing from stdin
. I don't like it though, and I feel it could be further simplified. What do you think?
use std::io;
use std::str;
fn read_thing<T: str::FromStr>() -> Result<T, &'static str> {
let mut buffer = String::new();
match io::stdin().read_line(&mut buffer) {
Ok(_) => match buffer.trim().parse() {
Ok(n) => Ok(n),
Err(_) => Err("Failed parsing input.")
},
Err(_) => Err("Failed reading from stdin."),
}
}