How do I explicitely use `is_sorted` from `is_sorted::IsSorted` and not from `std`?

The function Iterator::is_sorted is still unstable. I wanted to use the crate is_sorted that provides the same functionality. However, if I do foo.iter().is_sorted(), the compiler tries to use the one from std (and complains that it is still unstable and that didn't opt-in using unstable feature) even though I added a use is_sorted::IsSorted; at the top of my file (and the linter warns that the use statement isn't used). Is there some kind of sytax like foo.iter().is_sorted::IsSorted::is_sorted() to explicitely use the function is_sorted() from the trait is_sorted::IsSorted and not std::Iterator?

IsSorted::is_sorted(iter);

(This is called universal function call syntax, UFCS)

https://doc.rust-lang.org/book/ch19-03-advanced-traits.html?highlight=universal,function,call,syntax#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name

1 Like

This means that I need to split my iterator chain in 2?

I feel that I can't write:

foo.iter().map(...).IsSorted::is_sorted()`

So I must change it too

let mut iter = foo.iter().map(...);
IsSorted::is_sorted(&mut iter)

Right?

EDIT: forgot a mut.

Yes, unfortunately you can't specify that in a method chain. Another option is to create your own extension trait that uses a different name.

impl<I: IsSorted> IterExt for I {}
pub trait IterExt: IsSorted {
    fn is_sorted_ext(self) -> bool { IsSorted::is_sorted(self) }
}

foo.iter().map(...).is_sorted_ext()
1 Like

Thanks.

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.