@@ -498,7 +498,7 @@ mod prim_pointer {}
498
498
/// - [`Copy`]
499
499
/// - [`Clone`]
500
500
/// - [`Debug`]
501
- /// - [`IntoIterator`] (implemented for `&[T; N]` and `&mut [T; N]`)
501
+ /// - [`IntoIterator`] (implemented for `[T; N]`, ` &[T; N]` and `&mut [T; N]`)
502
502
/// - [`PartialEq`], [`PartialOrd`], [`Eq`], [`Ord`]
503
503
/// - [`Hash`]
504
504
/// - [`AsRef`], [`AsMut`]
@@ -517,7 +517,8 @@ mod prim_pointer {}
517
517
///
518
518
/// # Examples
519
519
///
520
- /// ```
520
+ #[ cfg_attr( bootstrap, doc = "```ignore" ) ]
521
+ #[ cfg_attr( not( bootstrap) , doc = "```" ) ]
521
522
/// let mut array: [i32; 3] = [0; 3];
522
523
///
523
524
/// array[1] = 1;
@@ -526,31 +527,16 @@ mod prim_pointer {}
526
527
/// assert_eq!([1, 2], &array[1..]);
527
528
///
528
529
/// // This loop prints: 0 1 2
529
- /// for x in & array {
530
+ /// for x in array {
530
531
/// print!("{} ", x);
531
532
/// }
532
533
/// ```
533
534
///
534
- /// An array itself is not iterable:
535
- ///
536
- /// ```compile_fail,E0277
537
- /// let array: [i32; 3] = [0; 3];
538
- ///
539
- /// for x in array { }
540
- /// // error: the trait bound `[i32; 3]: std::iter::Iterator` is not satisfied
541
- /// ```
542
- ///
543
- /// The solution is to coerce the array to a slice by calling a slice method:
535
+ /// You can also iterate over reference to the array's elements:
544
536
///
545
537
/// ```
546
- /// # let array: [i32; 3] = [0; 3];
547
- /// for x in array.iter() { }
548
- /// ```
549
- ///
550
- /// You can also use the array reference's [`IntoIterator`] implementation:
538
+ /// let array: [i32; 3] = [0; 3];
551
539
///
552
- /// ```
553
- /// # let array: [i32; 3] = [0; 3];
554
540
/// for x in &array { }
555
541
/// ```
556
542
///
@@ -564,6 +550,57 @@ mod prim_pointer {}
564
550
/// move_away(roa);
565
551
/// ```
566
552
///
553
+ /// # Editions
554
+ ///
555
+ /// Prior to Rust 1.53, arrays did not implement `IntoIterator` by value, so the method call
556
+ /// `array.into_iter()` auto-referenced into a slice iterator. That behavior is preserved in the
557
+ /// 2015 and 2018 editions of Rust for compatability, ignoring `IntoIterator` by value.
558
+ ///
559
+ #[ cfg_attr( bootstrap, doc = "```rust,edition2018,ignore" ) ]
560
+ #[ cfg_attr( not( bootstrap) , doc = "```rust,edition2018" ) ]
561
+ /// # #![allow(array_into_iter)] // override our `deny(warnings)`
562
+ /// let array: [i32; 3] = [0; 3];
563
+ ///
564
+ /// // This creates a slice iterator, producing references to each value.
565
+ /// for item in array.into_iter().enumerate() {
566
+ /// let (i, x): (usize, &i32) = item;
567
+ /// println!("array[{}] = {}", i, x);
568
+ /// }
569
+ ///
570
+ /// // The `array_into_iter` lint suggests this change for future compatibility:
571
+ /// for item in array.iter().enumerate() {
572
+ /// let (i, x): (usize, &i32) = item;
573
+ /// println!("array[{}] = {}", i, x);
574
+ /// }
575
+ ///
576
+ /// // You can explicitly iterate an array by value using
577
+ /// // `IntoIterator::into_iter` or `std::array::IntoIter::new`:
578
+ /// for item in IntoIterator::into_iter(array).enumerate() {
579
+ /// let (i, x): (usize, i32) = item;
580
+ /// println!("array[{}] = {}", i, x);
581
+ /// }
582
+ /// ```
583
+ ///
584
+ /// Starting in the 2021 edition, `array.into_iter()` will use `IntoIterator` normally to iterate
585
+ /// by value, and `iter()` should be used to iterate by reference like previous editions.
586
+ ///
587
+ /// ```rust,edition2021,ignore
588
+ /// # // FIXME: ignored because 2021 testing is still unstable
589
+ /// let array: [i32; 3] = [0; 3];
590
+ ///
591
+ /// // This iterates by reference:
592
+ /// for item in array.iter().enumerate() {
593
+ /// let (i, x): (usize, &i32) = item;
594
+ /// println!("array[{}] = {}", i, x);
595
+ /// }
596
+ ///
597
+ /// // This iterates by value:
598
+ /// for item in array.into_iter().enumerate() {
599
+ /// let (i, x): (usize, i32) = item;
600
+ /// println!("array[{}] = {}", i, x);
601
+ /// }
602
+ /// ```
603
+ ///
567
604
/// [slice]: prim@slice
568
605
/// [`Debug`]: fmt::Debug
569
606
/// [`Hash`]: hash::Hash
0 commit comments