/// ```
#[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
///
/// 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
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
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]