Skip to content

Commit 6f4fc43

Browse files
committed
Document the edition behavior for array.into_iter()
1 parent 4f98e23 commit 6f4fc43

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

core/src/array/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ impl<T, const N: usize> IntoIterator for [T; N] {
164164
type Item = T;
165165
type IntoIter = IntoIter<T, N>;
166166

167+
/// Creates a consuming iterator, that is, one that moves each value out of
168+
/// the array (from start to end). The array cannot be used after calling
169+
/// this unless `T` implements `Copy`, so the whole array is copied.
170+
///
171+
/// Arrays have special behavior when calling `.into_iter()` prior to the
172+
/// 2021 edition -- see the [array] Editions section for more information.
173+
///
174+
/// [array]: prim@array
167175
fn into_iter(self) -> Self::IntoIter {
168176
IntoIter::new(self)
169177
}

std/src/primitive_docs.rs

+50
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,56 @@ mod prim_pointer {}
549549
/// move_away(roa);
550550
/// ```
551551
///
552+
/// # Editions
553+
///
554+
/// Prior to Rust 1.53, arrays did not implement `IntoIterator` by value, so the method call
555+
/// `array.into_iter()` auto-referenced into a slice iterator. That behavior is preserved in the
556+
/// 2015 and 2018 editions of Rust for compatability, ignoring `IntoIterator` by value.
557+
///
558+
/// ```rust,edition2018
559+
/// # #![allow(array_into_iter)] // override our `deny(warnings)`
560+
/// let array: [i32; 3] = [0; 3];
561+
///
562+
/// // This creates a slice iterator, producing references to each value.
563+
/// for item in array.into_iter().enumerate() {
564+
/// let (i, x): (usize, &i32) = item;
565+
/// println!("array[{}] = {}", i, x);
566+
/// }
567+
///
568+
/// // The `array_into_iter` lint suggests this change for future compatibility:
569+
/// for item in array.iter().enumerate() {
570+
/// let (i, x): (usize, &i32) = item;
571+
/// println!("array[{}] = {}", i, x);
572+
/// }
573+
///
574+
/// // You can explicitly iterate an array by value using
575+
/// // `IntoIterator::into_iter` or `std::array::IntoIter::new`:
576+
/// for item in IntoIterator::into_iter(array).enumerate() {
577+
/// let (i, x): (usize, i32) = item;
578+
/// println!("array[{}] = {}", i, x);
579+
/// }
580+
/// ```
581+
///
582+
/// Starting in the 2021 edition, `array.into_iter()` will use `IntoIterator` normally to iterate
583+
/// by value, and `iter()` should be used to iterate by reference like previous editions.
584+
///
585+
/// ```rust,edition2021,ignore
586+
/// # // FIXME: ignored because 2021 testing is still unstable
587+
/// let array: [i32; 3] = [0; 3];
588+
///
589+
/// // This iterates by reference:
590+
/// for item in array.iter().enumerate() {
591+
/// let (i, x): (usize, &i32) = item;
592+
/// println!("array[{}] = {}", i, x);
593+
/// }
594+
///
595+
/// // This iterates by value:
596+
/// for item in array.into_iter().enumerate() {
597+
/// let (i, x): (usize, i32) = item;
598+
/// println!("array[{}] = {}", i, x);
599+
/// }
600+
/// ```
601+
///
552602
/// [slice]: prim@slice
553603
/// [`Debug`]: fmt::Debug
554604
/// [`Hash`]: hash::Hash

0 commit comments

Comments
 (0)