Hey, I wrote a function that reruns an iterator over a vector after some filtering. Since this function needs to be used in a context where I have a mutable reference on the vector (and I don't want to iterate over a copy of it), I wrote:
pub fn iter_free_spots<'a>(&'a mut self) -> impl Iterator<Item = (usize, usize)> + 'a {
self.v.iter()
.enumerate()
.filter(|e| *e.1 == Spot::Empty)
.map(|e| {
self.get_tuple_index(e.0)
})
}
it should return the indices of the free spots. as self.v
is defined as Vec<Spot>
and Spot
is an enum.
rust (version 1.43.1) gives me:
error[E0373]: closure may outlive the current function, but it borrows `self`, which is owned by the current function
--> src/board.rs:100:14
|
100 | .map(|e| {
| ^^^ may outlive borrowed value `self`
101 | self.get_tuple_index(e.0)
| ---- `self` is borrowed here
|
note: closure is returned here
--> src/board.rs:97:9
|
97 | / self.v.iter()
98 | | .enumerate()
99 | | .filter(|e| *e.1 == Spot::Empty)
100 | | .map(|e| {
101 | | self.get_tuple_index(e.0)
102 | | })
| |__________^
help: to force the closure to take ownership of `self` (and any other referenced variables), use the `move` keyword
|
100 | .map(move |e| {
| ^^^^^^^^
which is an amazing error message, but when I move
e
(as the help suggests) I get:
error[E0373]: closure may outlive the current function, but it borrows `self`, which is owned by the current function
--> src/board.rs:100:14
|
100 | .map(|e| {
| ^^^ may outlive borrowed value `self`
101 | self.get_tuple_index(e.0)
| ---- `self` is borrowed here
|
note: closure is returned here
--> src/board.rs:97:9
|
97 | / self.v.iter()
98 | | .enumerate()
99 | | .filter(|e| *e.1 == Spot::Empty)
100 | | .map(|e| {
101 | | self.get_tuple_index(e.0)
102 | | })
| |__________^
help: to force the closure to take ownership of `self` (and any other referenced variables), use the `move` keyword
|
100 | .map(move |e| {
| ^^^^^^^^
I guess I should tell the compiler that e (should be of type (usize, Spot)
) should have the same lifetime as self
.
is this correct?
If so, how can this be declared?