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: usizeThe 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
impl SizeHint
Sourcepub const UNIVERSAL: Self
pub const UNIVERSAL: Self
A size hint that is always valid, never changes, and conveys no information.
Sourcepub const fn try_new(
lower: usize,
upper: Option<usize>,
) -> Result<Self, InvalidSizeHint>
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");Sourcepub const fn try_bounded(
lower: usize,
upper: usize,
) -> Result<Self, InvalidSizeHint>
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");Sourcepub const fn unbounded(lower: usize) -> Self
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);Sourcepub const fn exact(len: usize) -> Self
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));Sourcepub const fn at_most(upper: usize) -> Self
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));Sourcepub const fn lower(self) -> usize
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);Sourcepub const fn upper(self) -> Option<usize>
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));Sourcepub const fn as_hint(self) -> (usize, Option<usize>)
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)));Sourcepub const fn overlaps(self, other: Self) -> bool
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");Sourcepub const fn disjoint(self, other: Self) -> bool
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");Sourcepub const fn subset_of(self, other: Self) -> bool
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 From<RangeToInclusive<usize>> for SizeHint
impl From<RangeToInclusive<usize>> for SizeHint
Source§fn from(range: RangeToInclusive<usize>) -> Self
fn from(range: RangeToInclusive<usize>) -> Self
Source§impl RangeBounds<usize> for SizeHint
A SizeHint represents a range of possible iterator lengths.
impl RangeBounds<usize> for SizeHint
A SizeHint represents a range of possible iterator lengths.
Source§fn start_bound(&self) -> Bound<&usize>
fn start_bound(&self) -> Bound<&usize>
Returns the smallest possible iterator length. Always Bound::Included.
Source§fn end_bound(&self) -> Bound<&usize>
fn end_bound(&self) -> Bound<&usize>
Returns the largest possible iterator length. Either Bound::Included or Bound::Unbounded.