What is the best way to slice a Vec of u32s (or any generic type) to the first occurrence of a particular number?
Naive, un-rusty method demonstrating what I want to do:
fn main() {
let v = vec![1, 2, 3, 4, 5, 6];
let to_num = 5;
let mut idx = 0;
for x in &v {
if x != &to_num {
idx += 1
} else {
break;
}
}
let slice = &v[..idx];
println!("{:?}", slice); //prints [1, 2, 3, 4]
}
>Solution :
You can use <[T]>::split():
let slice = v.split(|x| *x == to_num).next().unwrap();