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
first() in iterator #2833
Comments
There is such a method; it is just named |
Ah! |
It might be sufficient to add a note about |
it is not possible for all iterators to access the first element at any time. |
@aristarh2704 are you making a distinction between the “first ever” and the “first remaining”? Your statement is true for the former, but not really the latter. The latter is what seems to be being discussed here. Even for the former, you could have an iterator adapter that saves the first ever seen element and use that on top of any other iterator. |
@pnkfelix I somehow disagree. trait Iterator {
// …
fn first(self) -> Option<Self::Item> {
self.next()
}
} |
I like @phaazon's suggestion to add
|
I don’t want to do that, I support doing that. Also, most containers in Rust have a |
Keep in mind that this bloats every Rust binary that uses dyn Iterator, so I'd be careful adding stuff that is technically not needed. |
@CryZe That is not the case, because the methods are marked as |
Also… I’m not even sure it’s a real and useful argument. If the Rust team cared about that, the |
Honestly, the only version of |
Ideally, |
There is no way to know the very first element of an iterators, and for many iterators (like I.e. iterators can't track it's history in any way in general. |
Also fn main() {
let mut m = [1, 2, 3, 4, 5].iter();
m.next_back();
dbg!(m.last()); // Some(4)
} |
What about something more along the lines of Of course, you might want to have both those and |
@Saffith at that point you may as well just collect the iterator into a |
You could clone the iterator to "remember the initial state". let mut m = [1, 2, 3, 4, 5].iter();
let m_original = m.clone();
m.next_back();
assert_eq!(m_original.last(), Some(&5)); The struct PeekableIterator<I> {
original: I,
current: I,
}
impl<I: Clone> PeekableIterator<I> {
pub fn new(iter: I) -> Self {
Self {
original: iter.clone(),
current: iter,
}
}
pub fn reset(&mut self) {
self.current = self.original.clone();
}
}
impl<I: Iterator> Iterator for PeekableIterator<I> {
type Item = I::Item;
fn next(&mut self) -> Option<I::Item> {
self.current.next()
}
}
// delegate all other traits to `self.current`. |
|
An idea for an alternative way to approach this: https://internals.rust-lang.org/t/idea-diagnostic-aliases-attribute/13366?u=scottmcm |
As a newcomer to rust, I suggest that if you cannot always get the very first element of the iterator upon its creation, then the suggested implementation of .first() would just be confusing and not what I would expect. If it does not exist in the data structure, then we should not cover up that fact. |
std::iter::Iterator
has a method to get the last element,last()
. But why isn't there afirst()
to get the first element?There are times when
first()
comes handy, especially after filtering and/or sorting the items. Though it's possible to usenth(0)
in such cases,first()
would be cleaner.The text was updated successfully, but these errors were encountered: