I am trying to find repetitions in an iterable sequence. Furthermore, I want to know the elements that occurred in that sequence up to that point.
I created a HashMap
and am trying to call insert
on it from within a closure used by take_while
. However, I have so far not managed to get it to compile due to type mismatches related to concrete / bound lifetimes.
Here is a simplified version of my code which exhibits the same error:
use std::collections::HashSet;
fn main() {
let mut seq = HashSet::new();
let mut insert = |k| seq.insert(k);
(1..10).cycle().take_while(insert);
}
Here are the errors I get:
error[E0631]: type mismatch in closure arguments
--> src/main.rs:6:21
|
5 | let mut insert = |k| seq.insert(k);
| ----------------- found signature of `fn(_) -> _`
6 | (1..10).cycle().take_while(insert);
| ^^^^^^^^^^ expected signature of `for<'r> fn(&'r {integer}) -> _`
error[E0271]: type mismatch resolving `for<'r> <[closure@src/main.rs:5:22: 5:39 seq:_] as std::ops::FnOnce<(&'r {integer},)>>::Output == bool`
--> src/main.rs:6:21
|
6 | (1..10).cycle().take_while(insert);
| ^^^^^^^^^^ expected bound lifetime parameter, found concrete lifetime
How do I need to change the code for it to work?