Skip to main content

SizeHint

Struct SizeHint 

Source
pub struct SizeHint {
    pub lower: usize,
    pub upper: Option<usize>,
}
Expand description

A size hint for an iterator.

This is an immutable wrapper around the standard iterator size hint tuple (usize, Option<usize>), providing strong gurantees about the bound values (ie. lower <= upper), and additional functionality and conversions.

A size hint describes the range of possible values for the length of an iterator, that is, the number of elements that it will return when enumerated to exhaustion from its current state. While this count does not have to be exact (unless the size hint describes an exact bounds, one where the upper and lower bounds are equal) it is an error for the iterator to then produce a number of elements that violates the bounds established by the hint.

A size hint can never describe an empty range, as 0 is always a valid number of elements remaining for an iterator.

Fields§

§lower: usize

The inclusive lower bound of the size hint.

§upper: Option<usize>

The inclusive upper bound of the size hint, or None if the upper bound is unbounded.

Implementations§

Source§

impl SizeHint

Source

pub const UNIVERSAL: Self

A size hint that is always valid, never changes, and conveys no information.

Source

pub const ZERO: Self

A size hint that indicates that the iterator will yield no elements.

Source

pub const fn new(lower: usize, upper: Option<usize>) -> Self

Creates a new size hint with the given lower and optional upper bounds.

§Panics

Panics if the upper bound is present and less than the lower bound.

§Examples
let hint = SizeHint::new(5, Some(10));
assert_eq!(hint.lower, 5);
assert_eq!(hint.upper, Some(10));
Source

pub const fn try_new( lower: usize, upper: Option<usize>, ) -> Result<Self, InvalidSizeHint>

Tries to create a new size hint with the given lower and optional upper bounds.

§Errors

Returns InvalidSizeHint if the upper bound is present and less than the lower bound.

§Examples
let hint = SizeHint::try_new(5, Some(10))?;
assert_eq!(hint.lower, 5);
assert_eq!(hint.upper, Some(10));

let err: InvalidSizeHint = SizeHint::try_new(10, Some(5)).expect_err("Lower bound is greater than upper bound");
Source

pub const fn bounded(lower: usize, upper: usize) -> Self

Creates a new size hint with the given lower and optional upper bounds.

§Panics

Panics if lower is greater than upper.

§Examples
let hint = SizeHint::bounded(5, 10);
assert_eq!(hint.lower, 5);
assert_eq!(hint.upper, Some(10));
Source

pub const fn try_bounded( lower: usize, upper: usize, ) -> Result<Self, InvalidSizeHint>

Tries to create a new bounded SizeHint with the given lower and upper bounds.

§Errors

Returns InvalidSizeHint if lower is greater than upper.

§Examples
let hint = SizeHint::try_bounded(5, 10)?;
assert_eq!(hint.lower, 5);
assert_eq!(hint.upper, Some(10));

let err: InvalidSizeHint = SizeHint::try_bounded(10, 5).expect_err("SizeHint should be invalid");
Source

pub const fn unbounded(lower: usize) -> Self

Creates a new size hint with the given lower bound and no upper bound.

let hint = SizeHint::unbounded(5);
assert_eq!(hint.lower, 5);
assert_eq!(hint.upper, None);
Source

pub const fn exact(len: usize) -> Self

Creates a new size hint with an exact count.

let hint = SizeHint::exact(5);
assert_eq!(hint.lower, 5);
assert_eq!(hint.upper, Some(5));
Source

pub const fn at_most(upper: usize) -> Self

Creates a new size hint with the given upper bound and a lower bound of 0.

A common use case for this is Filter.

let hint = SizeHint::at_most(5);
assert_eq!(hint.lower, 0);
assert_eq!(hint.upper, Some(5));
Source

pub const fn lower(self) -> usize

Returns the inclusive lower bound of the size hint.

§Examples
let hint = SizeHint::new(5, Some(10));
assert_eq!(hint.lower(), 5);
Source

pub const fn upper(self) -> Option<usize>

Returns the optional inclusive upper bound of the size hint.

§Examples
let hint = SizeHint::new(5, Some(10));
assert_eq!(hint.upper(), Some(10));
Source

pub const fn as_hint(self) -> (usize, Option<usize>)

Returns the size hint as a tuple (lower, upper).

§Examples
let hint = SizeHint::new(5, Some(10));
assert_eq!(hint.as_hint(), (5, Some(10)));
Source

pub fn decrement(self) -> Self

Returns a new SizeHint with the lower and upper bounds (if present) decremented by 1.

This is useful for decrementing the size hint of an iterator after it has been advanced.

§Examples
let hint = SizeHint::bounded(5, 10);
assert_eq!(hint.decrement(), SizeHint::bounded(4, 9));
Source

pub const fn overlaps(self, other: Self) -> bool

Returns true if this size hint range overlaps with another size hint range.

Two ranges overlap if there exists at least one value that could be contained in both. This operation is the negation of Self::disjoint, and is also commutative.

§Examples
assert!(SizeHint::overlaps(SizeHint::bounded(3, 6), SizeHint::bounded(5, 10)), "should overlap at 5 and 6");
assert!(SizeHint::overlaps(SizeHint::exact(5), SizeHint::UNIVERSAL), "Universal should overlap with any size hint");
assert!(!SizeHint::overlaps(SizeHint::unbounded(11), SizeHint::exact(5)), "should not overlap");
assert!(SizeHint::overlaps(SizeHint::unbounded(11), SizeHint::UNIVERSAL), "two unbounded hints should overlap");
Source

pub const fn disjoint(self, other: Self) -> bool

Returns true if this size hint range is disjoint with another range.

Two ranges are disjoint if there exists no value that could be contained in both. This operation is the negation of Self::overlaps, and is also commutative.

§Examples
assert!(SizeHint::disjoint(SizeHint::exact(5), SizeHint::unbounded(10)), "should be disjoint");
assert!(SizeHint::disjoint(SizeHint::exact(5), SizeHint::bounded(6, 10)), "should be disjoint");
assert!(!SizeHint::disjoint(SizeHint::unbounded(11), SizeHint::UNIVERSAL), "two unbounded hints should never be disjoint");
Source

pub const fn subset_of(self, other: Self) -> bool

Returns true if this size hint range is completely contained within another range.

This operation is not commutative, i.e. a.subset_of(b) does not imply b.subset_of(a).

§Examples
assert!(SizeHint::bounded(4, 6).subset_of(SizeHint::bounded(4, 6)), "should be a subset (equal ranges)");
assert!(SizeHint::bounded(4, 6).subset_of(SizeHint::bounded(3, 9)), "should be a subset");
assert!(!SizeHint::bounded(3, 9).subset_of(SizeHint::bounded(4, 6)), "should not be a subset (not commutative)");
assert!(!SizeHint::bounded(2, 6).subset_of(SizeHint::bounded(3, 6)), "should not be a subset (lower bound)");
assert!(!SizeHint::bounded(4, 7).subset_of(SizeHint::bounded(3, 6)), "should not be a subset (upper bound)");

Trait Implementations§

Source§

impl Clone for SizeHint

Source§

fn clone(&self) -> SizeHint

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SizeHint

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SizeHint

Source§

fn default() -> SizeHint

Returns the “default value” for a type. Read more
Source§

impl From<RangeFrom<usize>> for SizeHint

Source§

fn from(range: RangeFrom<usize>) -> Self

Converts to this type from the input type.
Source§

impl From<RangeFull> for SizeHint

Source§

fn from(_: RangeFull) -> Self

Converts to this type from the input type.
Source§

impl From<RangeToInclusive<usize>> for SizeHint

Source§

fn from(range: RangeToInclusive<usize>) -> Self

Converts to this type from the input type.
Source§

impl From<SizeHint> for (usize, Option<usize>)

Source§

fn from(hint: SizeHint) -> Self

Converts to this type from the input type.
Source§

impl Hash for SizeHint

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq<(usize, Option<usize>)> for SizeHint

Source§

fn eq(&self, other: &(usize, Option<usize>)) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<SizeHint> for (usize, Option<usize>)

Source§

fn eq(&self, other: &SizeHint) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for SizeHint

Source§

fn eq(&self, other: &SizeHint) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl RangeBounds<usize> for SizeHint

A SizeHint represents a range of possible iterator lengths.

Source§

fn start_bound(&self) -> Bound<&usize>

Returns the smallest possible iterator length. Always Bound::Included.

Source§

fn end_bound(&self) -> Bound<&usize>

Returns the largest possible iterator length. Either Bound::Included or Bound::Unbounded.

1.35.0 · Source§

fn contains<U>(&self, item: &U) -> bool
where T: PartialOrd<U>, U: PartialOrd<T> + ?Sized,

Returns true if item is contained in the range. Read more
Source§

fn is_empty(&self) -> bool
where T: PartialOrd,

🔬This is a nightly-only experimental API. (range_bounds_is_empty)
Returns true if the range contains no items. One-sided ranges (RangeFrom, etc) always return false. Read more
Source§

impl TryFrom<(usize, Option<usize>)> for SizeHint

Source§

type Error = InvalidSizeHint

The type returned in the event of a conversion error.
Source§

fn try_from(hint: (usize, Option<usize>)) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Range<usize>> for SizeHint

Source§

type Error = InvalidSizeHint

The type returned in the event of a conversion error.
Source§

fn try_from(range: Range<usize>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<RangeInclusive<usize>> for SizeHint

Source§

type Error = InvalidSizeHint

The type returned in the event of a conversion error.
Source§

fn try_from(range: RangeInclusive<usize>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<RangeTo<usize>> for SizeHint

Source§

type Error = InvalidSizeHint

The type returned in the event of a conversion error.
Source§

fn try_from(range: RangeTo<usize>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for SizeHint

Source§

impl Eq for SizeHint

Source§

impl StructuralPartialEq for SizeHint

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoOption for T

Source§

fn into_some(self) -> Option<Self>
where Self: Sized,

Moves a value into a Some variant of Option. Read more
Source§

fn into_none<TOut>(self) -> Option<TOut>
where Self: Sized,

Turns a value into None. Read more
Source§

impl<T> IntoResult for T

Source§

fn into_ok<E>(self) -> Result<Self, E>
where Self: Sized,

Moves a value into an Ok variant of a Result. Read more
Source§

fn into_err<T>(self) -> Result<T, Self>
where Self: Sized,

Move a error value into an Err variant of a Result. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.