Trait for std::slice

Hi,
I am new in Rust, trying to export #![feature(slice_group_by)] method into a separate helper but have a problem with implementing trait for [T] slice.
I appreciate any directions.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3c1cf99d225728a4286e4fafe1028d41

pub trait MyTrait<T> {
    fn group_by<F>(&self, pred: F) -> GroupBy<'_, T, F>;
}

impl<'a, T> MyTrait<T> for std::slice::[T]<'a, T> {
    fn group_by<F>(&self, pred: F) -> GroupBy<'_, T, F>
    where
        F: FnMut(&T, &T) -> bool,
    {
        GroupBy::new(self, pred)
    }
}

[T] is a built-in type, which you can use like this:

impl<T> MyTrait<T> for [T] { ... }

(Playground)

5 Likes

works! thank you!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.