Trait core::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 I: TrustedLen<Item = &'a T>, T: Clone + 'a,

1.36.0 · source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

1.43.0 · source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

impl<I> TrustedLen for Flatten<I>where I: Iterator<Item: IntoIterator>, FlattenCompat<I, <I::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, U, F> TrustedLen for FlatMap<I, U, F>where I: Iterator, U: IntoIterator, F: FnMut(I::Item) -> U, FlattenCompat<Map<I, F>, <U as IntoIterator>::IntoIter>: TrustedLen,

source§

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

source§

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

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 core::slice::Iter<'_, T>

source§

impl<T> TrustedLen for core::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>

1.40.0 · source§

impl<T, const N: usize> TrustedLen for core::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>