I have a file containing prime numbers that I'm unpacking into a u32
vector. I'm curious if I can do this in one step. If I unpack contents into a str
vector, then an u32
vector, everything's dandy.
If I try to move everything into a u32
vector as I try to do in
v_int2
, I get the error that
|&x| doesn't have a known size at compile time.
My current understanding is that &str
has a known size at compile time, since &str
are written into the binary. Skipping this step means I'm parsing from String
to u32
, and I need to inform the compiler about something having to do with the size of my elements. I'm not aware of a way around this, though I'm new in Rust town.
let mut file = File::open("first10kprimes.txt")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let v_str = contents.split_whitespace().collect::<Vec<&str>>(); // I just discovered I could do that typing after collect, was a lil jazzed
let v_int: Vec<u32> = v_str.iter().map(|&x| x.parse::<u32>().unwrap()).collect();
let v_int2: Vec<u32> = contents
.split_whitespace()
.map(|&x| x.parse().unwrap())
.collect();