Skip to content

Potential specialisation: DoubleEndedIterator::last #41691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
clarfonthey opened this issue May 2, 2017 · 1 comment
Closed

Potential specialisation: DoubleEndedIterator::last #41691

clarfonthey opened this issue May 2, 2017 · 1 comment

Comments

@clarfonthey
Copy link
Contributor

clarfonthey commented May 2, 2017

Right now, Iterator::last defaults to:

let mut last = None;
for x in self { last = Some(x); }
last

If the iterator implements DoubleEndedIterator, it should default to:

self.next_back()

Many standard library types manually implement this specialisation, but a blanket implementation would be nice too!

Another potential specialisation for nth with DoubleEndedIterator and ExactSizeIterator:

if self.len() - n < n {
    for x in self.rev() {
        if n == 0 { return Some(x) }
        n -= 1;
    }
} else {
    for x in self {
        if n == 0 { return Some(x) }
        n -= 1;
    }
}
None

There's a bunch of stuff here and it might be worth looking into what extra implementations the standard library has, and how we could potentially make this default using specialisation.

@Mark-Simulacrum
Copy link
Member

Closing as per #42584 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants