Hi there.
I'm new to Rust so I thought I start with something simple like Input from Stdin and convert the input into a u32
Here's the working code
std::io;
fn get_input() -> Result<u32,u32> {
let mut buffer = String::new();
io::stdin().read_line(&mut buffer).unwrap();
match buffer.trim().parse::<u32>() {
Ok(n) => Ok(n),
Err(_) => Err(0),
}
}
fn main() {
println!("{:?}" , get_input());
}
But if I try to make this function generic, so that it returns other types then u32, I get an E0277 error..
use std::io;
fn get_input<T>() -> Result<T,u32> {
let mut buffer = String::new();
io::stdin().read_line(&mut buffer).unwrap();
match buffer.trim().parse::<T>() {
Ok(n) => Ok(n),
Err(_) => Err(0),
}
}
fn main() {
println!("{:?}" , get_input::<u32>());
}
How do I solve this problem ?
thank's in advance