Skip to content

Commit 6ac4698

Browse files
authored
Rollup merge of rust-lang#84590 - est31:array_into_iter, r=nikomatsakis
Point out that behavior might be switched on 2015 and 2018 too one day Reword documentation to make it clear that behaviour can be switched on older editions too, one day in the future. It doesn't *have* to be switched, but I think it's good to have it as an option and re-evaluate it a few months/years down the line when e.g. the crates that showed up in crater were broken by different changes in the language already. cc rust-lang#25725, rust-lang#65819, rust-lang#66145, rust-lang#84147 , and rust-lang#84133 (comment)
2 parents 1ecc464 + 12642d9 commit 6ac4698

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

library/std/src/primitive_docs.rs

+46-2
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,10 @@ mod prim_pointer {}
553553
/// # Editions
554554
///
555555
/// 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.
556+
/// `array.into_iter()` auto-referenced into a slice iterator. Right now, the old behavior
557+
/// is preserved in the 2015 and 2018 editions of Rust for compatibility, ignoring
558+
/// `IntoIterator` by value. In the future, the behavior on the 2015 and 2018 edition
559+
/// might be made consistent to the behavior of later editions.
558560
///
559561
#[cfg_attr(bootstrap, doc = "```rust,edition2018,ignore")]
560562
#[cfg_attr(not(bootstrap), doc = "```rust,edition2018")]
@@ -601,6 +603,48 @@ mod prim_pointer {}
601603
/// }
602604
/// ```
603605
///
606+
/// Future language versions might start treating the `array.into_iter()`
607+
/// syntax on editions 2015 and 2018 the same as on edition 2021. So code using
608+
/// those older editions should still be written with this change in mind, to
609+
/// prevent breakage in the future. The safest way to accomplish this is to
610+
/// avoid the `into_iter` syntax on those editions. If an edition update is not
611+
/// viable/desired, there are multiple alternatives:
612+
/// * use `iter`, equivalent to the old behavior, creating references
613+
/// * use [`array::IntoIter`], equivalent to the post-2021 behavior (Rust 1.51+)
614+
/// * replace `for ... in array.into_iter() {` with `for ... in array {`,
615+
/// equivalent to the post-2021 behavior (Rust 1.53+)
616+
///
617+
/// ```rust,edition2018
618+
/// use std::array::IntoIter;
619+
///
620+
/// let array: [i32; 3] = [0; 3];
621+
///
622+
/// // This iterates by reference:
623+
/// for item in array.iter() {
624+
/// let x: &i32 = item;
625+
/// println!("{}", x);
626+
/// }
627+
///
628+
/// // This iterates by value:
629+
/// for item in IntoIter::new(array) {
630+
/// let x: i32 = item;
631+
/// println!("{}", x);
632+
/// }
633+
///
634+
/// // This iterates by value:
635+
/// for item in array {
636+
/// let x: i32 = item;
637+
/// println!("{}", x);
638+
/// }
639+
///
640+
/// // IntoIter can also start a chain.
641+
/// // This iterates by value:
642+
/// for item in IntoIter::new(array).enumerate() {
643+
/// let (i, x): (usize, i32) = item;
644+
/// println!("array[{}] = {}", i, x);
645+
/// }
646+
/// ```
647+
///
604648
/// [slice]: prim@slice
605649
/// [`Debug`]: fmt::Debug
606650
/// [`Hash`]: hash::Hash

0 commit comments

Comments
 (0)