-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Add is_empty
function to ExactSizeIterator
#34357
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -485,8 +485,6 @@ impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I { | |
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait ExactSizeIterator: Iterator { | ||
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
/// Returns the exact number of times the iterator will iterate. | ||
/// | ||
/// This method has a default implementation, so you usually should not | ||
|
@@ -510,6 +508,8 @@ pub trait ExactSizeIterator: Iterator { | |
/// | ||
/// assert_eq!(5, five.len()); | ||
/// ``` | ||
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
fn len(&self) -> usize { | ||
let (lower, upper) = self.size_hint(); | ||
// Note: This assertion is overly defensive, but it checks the invariant | ||
|
@@ -519,6 +519,31 @@ pub trait ExactSizeIterator: Iterator { | |
assert_eq!(upper, Some(lower)); | ||
lower | ||
} | ||
|
||
/// | ||
/// Returns whether the iterator is empty. | ||
/// | ||
/// This method has a default implementation using `self.len()`, so you | ||
/// don't need to implement it yourself. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Basic usage: | ||
/// | ||
/// ``` | ||
/// let mut one_element = [0].iter(); | ||
/// assert!(!one_element.is_empty()); | ||
/// | ||
/// assert_eq!(one_element.next(), Some(0)); | ||
/// assert!(one_element.is_empty()); | ||
/// | ||
/// assert_eq!(one_element.next(), None); | ||
/// ``` | ||
#[inline] | ||
#[unstable(feature = "exact_size_is_empty", issue = "0")] | ||
fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rustdoc might not like this leading empty doc comment line :)