Generics (or how do I solve an E0277 error)

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

1 Like

Read this chapter: Traits

1 Like

@stebalien already gave good advice.
To be more specific, unlike C++, you need to know what type 'can' to perform relevant operations over it. Check what are constraints over generic type in parse function and put same constraints on your type T in your code.

Hi, I solved it. The hint regarding the constraints in the parse function did the Trick.

use std::str::FromStr;                                                                                                                                                                                                                                                                    
fn get_input<T: FromStr>() -> Result<T,u32> { .......}

Still learning but getting better. :relieved:

1 Like