When trying to solve another question asked here, I came across this problem:
I am trying to get the intersection of HashSets.
With
let mut common_binaries: Box<dyn Iterator<Item = &str>> =
Box::new(binaries.pop().unwrap().into_iter());
for current_set in binaries {
common_binaries = Box::new(common_binaries.filter(|it| current_set.contains(*it)));
}
I get:
error[E0597]: `current_set` does not live long enough
--> src/main.rs:22:64
|
22 | common_binaries = Box::new(common_binaries.filter(|it| current_set.contains(*it)));
| ---- ^^^^^^^^^^^ borrowed value does not live long enough
| |
| value captured here
23 | }
| - `current_set` dropped here while still borrowed
...
30 | }
| - borrow might be used here, when `common_binaries` is dropped and runs the destructor for type `std::boxed::Box<dyn std::iter::Iterator<Item = &str>>`
|
= note: values in a scope are dropped in the opposite order they are defined
.
Note that the borrow is also used in the println
statement, so that it is not enough to define current_set
before common_binaries
.
But what's the problem? I mean, the closure only returns bool
which is Copy
!
(only out of interest, I do not need this code)