Furthering the example of implementing IntoIterator
for a wrapped vector as per the Rust book, I am also trying to implement IntoIterator for a reference to the wrapper, as per the following code (Playground link):
struct VecWrapper(Vec<i32>);
impl VecWrapper {
fn iter(&'static self) -> Iter {
Iter(Box::new(self.0.iter()))
}
}
struct Iter(Box<Iterator<Item = &'static i32>>);
impl Iterator for Iter {
type Item = &'static i32;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl IntoIterator for &'static VecWrapper {
type Item = &'static i32;
type IntoIter = Iter;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
fn main() {
// let test = vec![1, 2, 3]; // obviously, works
let test = VecWrapper(vec![1, 2, 3]); // not working
for v in &test {
println!("{}", v);
}
}
Although the implementation compiles, the attempt to use it in main
doesn't with the following error:
error[E0597]: `test` does not live long enough
--> src/main.rs:31:14
|
31 | for v in &test {
| ^^^^^
| |
| borrowed value does not live long enough
| argument requires that `test` is borrowed for `'static`
...
34 | }
| - `test` dropped here while still borrowed
This code is greatly simplified from what I would actually want to use as to using only 'static
lifetimes, using an existing contained type, and using i32
for the inner (iterated) type, but it is boiled down to show just the problem.
The accepted answer solves the first part of the problem as to not using 'static
and using + 'a
with traits. I still am having a problem with the actual code, which is a LazyList
implementation. I've posted that as Am I incorrectly implementing IntoIterator for a reference to a LazyList implementation or is this a Rust bug?.