Skip to content

Commit 12642d9

Browse files
committed
Add a paragraph with possible alternatives on older editions
1 parent 5bd3187 commit 12642d9

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

library/std/src/primitive_docs.rs

+42
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,48 @@ mod prim_pointer {}
603603
/// }
604604
/// ```
605605
///
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+
///
606648
/// [slice]: prim@slice
607649
/// [`Debug`]: fmt::Debug
608650
/// [`Hash`]: hash::Hash

0 commit comments

Comments
 (0)