Я пытаюсь реализовать итератор для двумерной сетки, как показано ниже (это упрощение немного более сложной настройки):
struct Grid {
width: usize,
height: usize,
}
impl Grid {
fn new(width: usize, height: usize) -> Grid {
Grid { width, height }
}
fn iter<'a>(&'a self) -> &'a impl Iterator<Item = (usize, usize)> {
let i = (0..self.height).flat_map(|y: usize| (0..self.width).map(move |x| (x, y)));
&i
}
}
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/lib.rs:12:43
|
12 | let i = (0..self.height).flat_map(|y: usize| (0..self.width).map(move |x| (x, y)));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime 'a as defined on the method body at 11:13...
--> src/lib.rs:11:13
|
11 | fn iter<'a>(&'a self) -> &'a impl Iterator<Item = (usize, usize)> {
| ^^
= note: ...so that the types are compatible:
expected &&Grid
found &&'a Grid
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that return value is valid for the call
--> src/lib.rs:11:34
|
11 | fn iter<'a>(&'a self) -> &'a impl Iterator<Item = (usize, usize)> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Я не могу понять, каквозвращать ссылку на итератор с соответствующим сроком жизниЯ понимаю, что время жизни итератора не должно превышать время жизни базовой Grid
структуры.