itertools icon indicating copy to clipboard operation
itertools copied to clipboard

unique_by used with a reference to Self::Item

Open nklhtv opened this issue 3 years ago • 0 comments

Hi, I am trying to avoid cloning data while using unique_by and returning a reference to Self::Item, but there is an issue with the lifetimes. How can i do that?

use itertools::Itertools;

#[derive(Debug, PartialEq)]
struct Foo(pub String);

fn main() {
    let data = vec![Foo("1".to_owned()), Foo("3".to_owned()), Foo("3".to_owned())];
    itertools::assert_equal(data.into_iter().unique_by(|foo| &foo.0),
                        vec![Foo("1".to_owned()), Foo("3".to_owned())]);
}

nklhtv avatar Jun 17 '22 08:06 nklhtv