Is it possible to take ownership of a vector, break it up and take specific items from it?
This does not compile ("cannot move out of index"):
fn main() {
let mut v: Vec<String> = Vec::with_capacity(400);
for i in 100..500 { v.push(String::from(i.to_string())); }
println!("{}", v.len());
let (a, b) = take_two(v);
println!("{}", a);
println!("{}", b);
}
fn take_two(v: Vec<String>) -> (String, String) {
(v[123], v[321])
}
I found it is effectively possible using this contraption:
fn main() {
let mut v: Vec<String> = Vec::with_capacity(400);
for i in 100..500 { v.push(String::from(i.to_string())); }
let (a, b) = take_two(v);
println!("{}", a);
println!("{}", b);
}
fn take_two(v: Vec<String>) -> (String, String) {
let it = v.into_iter();
let mut it = it.skip(123);
let a = it.next().unwrap();
let mut it = it.skip(198);
let b = it.next().unwrap();
(a, b)
}
Is there a more straightforward way?