This question is follow up on the Vector of iterators. It seems to be quite easy to store vec::IntoIter
inside a vector:
let seq: Vec<Vec<i32>> = vec![vec![0,1], vec![2,3]];
type IT = std::vec::IntoIter<i32>;
let mut iters: Vec<IT> = Vec::new();
let mut it = seq.into_iter();
loop {
match it.next() {
None => break,
Some(s) => {
iters.push( s.into_iter() );
},
};
}
But if the original vector is modified it means that iterators are invalidated (from my C++ background) so I think it makes sense to use LinkedList
or VecDeque
to be sure that iterators are always valid (please correct me if I'm wrong). The problem is that I don't get how to declare a type alias for VecDeque::Iter
and also I found that there is no IntoIter
in VecDeque
:
use std::collections::VecDeque;
let seq: Vec<Vec<i32>> = vec![vec![0,1], vec![2,3]];
type IT = std::collections::VecDeque::Iter; // Error!
let mut iters: Vec<IT> = Vec::new();
let mut it = seq.iter(); // no into_iter for VecDeque, might be wrong as well
loop {
match it.next() {
None => break,
Some(s) => {
iters.push( s.iter() ); // not sure that we can do this way
},
};
}
Background for this post: I'm learning Rust by rewriting some leetcode problems from C++ to Rust. In this case, I'm doing specifically problem "LRU Cache". It might mean that I'm doing something conceptually wrong just due to my C++ background.