Skip to content

Commit f706216

Browse files
Rollup merge of #81742 - sdroege:exact-size-iterator-correctness, r=kennytm
Add a note about the correctness and the effect on unsafe code to the `ExactSizeIterator` docs As it is a safe trait it does not provide any guarantee that the returned length is correct and as such unsafe code must not rely on it. That's why `TrustedLen` exists. Fixes #81739
2 parents 6e1f713 + f436630 commit f706216

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

library/core/src/iter/traits/exact_size.rs

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
/// implement it. However, you may be able to provide a more performant
1414
/// implementation than the default, so overriding it in this case makes sense.
1515
///
16+
/// Note that this trait is a safe trait and as such does *not* and *cannot*
17+
/// guarantee that the returned length is correct. This means that `unsafe`
18+
/// code **must not** rely on the correctness of [`Iterator::size_hint`]. The
19+
/// unstable and unsafe [`TrustedLen`](super::marker::TrustedLen) trait gives
20+
/// this additional guarantee.
21+
///
1622
/// [`len`]: ExactSizeIterator::len
1723
///
1824
/// # Examples

0 commit comments

Comments
 (0)