pub fn sample_single_from_known_length<I, R, T>(
rng: &mut R,
iter: I,
) -> Option<T>Expand description
Samples one element uniformly at random from an iterator whose length is known at runtime.
The caller must ensure that (len, Some(len)) == iter.size_hint(), i.e. the iterator
reports its exact length via size_hint. We do not require ExactSizeIterator
because that is a compile-time guarantee, whereas our requirement is a runtime condition.
The implementation selects a random index and uses Iterator::nth. For iterators
with O(1) nth (e.g., randomly indexable structures), this is very efficient.
The selected value is cloned.
The iterator need only support iteration; random indexing is not required. This function is intended for use when the result set is indexed and its length is known.