Is it possible to pass a primitive type (such as i32
) as a function argument.
For example, I want to read the user input and store it in a vector of my choosing with a function
use std::io;
fn main()
{
get_vectorized_line(i32) ;
}
fn get_vectorized_line(expected_type: type)->Vec<expected_type>{
let mut line_content=String::new();
io::stdin().read_line(&mut line_content).expect("Could not read line");
let vectorized_line: Vec<expected_type> = line_content.trim().split(" ").
map(|s| s.parse().expect("Could not parse")).collect();
return vectorized_line;
}
responds with
expected value, found builtin type i32
when the function is called.
I know it is possible to pass types as arguments in Python, (and not in standard C without using macros).
I believe I could be using generic functions to do what I want. But I wanted clarity on this topic