Trait std::iter::TrustedLen

source ·
pub unsafe trait TrustedLen: Iterator { }
🔬This is a nightly-only experimental API. (trusted_len #37572)
Expand description

一个使用 size_hint 报告准确长度的迭代器。

迭代器报告一个大小提示,该提示要么是精确的 (下限等于上限),要么上限是 None。 如果实际的迭代器长度大于 usize::MAX,则上限必须仅为 None。在这种情况下,下限必须是 usize::MAX,导致 (usize::MAX, None)Iterator::size_hint()

迭代器必须精确地生成它所报告或发散的元素数量,然后才能结束。

什么时候不应该适配器是 TrustedLen?

如果适配器使迭代器给定数量,那么该适配器实现 TrustedLen 通常是不正确的。 内部迭代器返回的可能多于 usize::MAX 项,但没有办法知道 k 小于该项的元素是多少,因为来自内部迭代器的 size_hint 已经饱和并丢失了该信息。

这就是 Skip<I> 不是 TrustedLen 的原因,即使 I 实现了 TrustedLen

Safety

只有在遵守契约的情况下才能实现 trait。这个 trait 的使用者必须检查 Iterator::size_hint () 的上限。

Implementors§

source§

impl TrustedLen for Bytes<'_>

source§

impl<'a, I, T> TrustedLen for Cloned<I>where T: 'a + Clone, I: TrustedLen<Item = &'a T>,

1.36.0 · source§

impl<'a, I, T> TrustedLen for Copied<I>where T: 'a + Copy, I: TrustedLen<Item = &'a T>,

source§

impl<A> TrustedLen for Range<A>where A: TrustedStep,

source§

impl<A> TrustedLen for RangeFrom<A>where A: TrustedStep,

source§

impl<A> TrustedLen for RangeInclusive<A>where A: TrustedStep,

source§

impl<A> TrustedLen for std::option::IntoIter<A>

source§

impl<A> TrustedLen for std::option::Iter<'_, A>

source§

impl<A> TrustedLen for std::option::IterMut<'_, A>

source§

impl<A> TrustedLen for std::result::IntoIter<A>

source§

impl<A> TrustedLen for std::result::Iter<'_, A>

source§

impl<A> TrustedLen for std::result::IterMut<'_, A>

source§

impl<A> TrustedLen for Repeat<A>where A: Clone,

source§

impl<A, B> TrustedLen for Chain<A, B>where A: TrustedLen, B: TrustedLen<Item = <A as Iterator>::Item>,

source§

impl<A, B> TrustedLen for Zip<A, B>where A: TrustedLen, B: TrustedLen,

1.43.0 · source§

impl<A, F> TrustedLen for OnceWith<F>where F: FnOnce() -> A,

source§

impl<A, F> TrustedLen for RepeatWith<F>where F: FnMut() -> A,

source§

impl<B, I, F> TrustedLen for Map<I, F>where I: TrustedLen, F: FnMut(<I as Iterator>::Item) -> B,

source§

impl<I> TrustedLen for &mut Iwhere I: TrustedLen + ?Sized,

source§

impl<I> TrustedLen for Enumerate<I>where I: TrustedLen,

source§

impl<I> TrustedLen for Flatten<I>where I: Iterator, <I as Iterator>::Item: IntoIterator, FlattenCompat<I, <<I as Iterator>::Item as IntoIterator>::IntoIter>: TrustedLen,

source§

impl<I> TrustedLen for Fuse<I>where I: TrustedLen,

source§

impl<I> TrustedLen for Peekable<I>where I: TrustedLen,

source§

impl<I> TrustedLen for Rev<I>where I: TrustedLen + DoubleEndedIterator,

source§

impl<I> TrustedLen for Take<I>where I: TrustedLen,

source§

impl<I, U, F> TrustedLen for FlatMap<I, U, F>where I: Iterator, U: IntoIterator, F: FnMut(<I as Iterator>::Item) -> U, FlattenCompat<Map<I, F>, <U as IntoIterator>::IntoIter>: TrustedLen,

source§

impl<T> TrustedLen for DrainSorted<'_, T>where T: Ord,

source§

impl<T> TrustedLen for IntoIterSorted<T>where T: Ord,

source§

impl<T> TrustedLen for std::collections::vec_deque::Iter<'_, T>

source§

impl<T> TrustedLen for std::collections::vec_deque::IterMut<'_, T>

source§

impl<T> TrustedLen for Chunks<'_, T>

source§

impl<T> TrustedLen for ChunksExact<'_, T>

source§

impl<T> TrustedLen for ChunksExactMut<'_, T>

source§

impl<T> TrustedLen for ChunksMut<'_, T>

source§

impl<T> TrustedLen for std::slice::Iter<'_, T>

source§

impl<T> TrustedLen for std::slice::IterMut<'_, T>

source§

impl<T> TrustedLen for RChunks<'_, T>

source§

impl<T> TrustedLen for RChunksExact<'_, T>

source§

impl<T> TrustedLen for RChunksExactMut<'_, T>

source§

impl<T> TrustedLen for RChunksMut<'_, T>

source§

impl<T> TrustedLen for Windows<'_, T>

source§

impl<T> TrustedLen for Empty<T>

source§

impl<T> TrustedLen for Once<T>

source§

impl<T, A> TrustedLen for std::collections::vec_deque::IntoIter<T, A>where A: Allocator,

source§

impl<T, A> TrustedLen for Drain<'_, T, A>where A: Allocator,

source§

impl<T, A> TrustedLen for std::vec::IntoIter<T, A>where A: Allocator,

1.40.0 · source§

impl<T, const N: usize> TrustedLen for std::array::IntoIter<T, N>

source§

impl<T, const N: usize> TrustedLen for ArrayChunks<'_, T, N>

source§

impl<T, const N: usize> TrustedLen for ArrayChunksMut<'_, T, N>