about summary refs log tree commit diff
path: root/library/std/src/primitive_docs.rs
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2021-04-28 01:33:16 +0200
committerest31 <MTest31@outlook.com>2021-04-28 01:42:14 +0200
commit12642d99a6a7f5302efb9079ece54cf18c337571 (patch)
treec31a4260405be3d5a38f1f29d17dca2277ef8b81 /library/std/src/primitive_docs.rs
parent5bd31879d7969ceb9d616bf715722ccf807c015d (diff)
downloadrust-12642d99a6a7f5302efb9079ece54cf18c337571.tar.gz
rust-12642d99a6a7f5302efb9079ece54cf18c337571.zip
Add a paragraph with possible alternatives on older editions
Diffstat (limited to 'library/std/src/primitive_docs.rs')
-rw-r--r--library/std/src/primitive_docs.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs
index ef0ab05a58d..a5292c8b878 100644
--- a/library/std/src/primitive_docs.rs
+++ b/library/std/src/primitive_docs.rs
@@ -603,6 +603,48 @@ mod prim_pointer {}
 /// }
 /// ```
 ///
+/// Future language versions might start treating the `array.into_iter()`
+/// syntax on editions 2015 and 2018 the same as on edition 2021. So code using
+/// those older editions should still be written with this change in mind, to
+/// prevent breakage in the future. The safest way to accomplish this is to
+/// avoid the `into_iter` syntax on those editions. If an edition update is not
+/// viable/desired, there are multiple alternatives:
+/// * use `iter`, equivalent to the old behavior, creating references
+/// * use [`array::IntoIter`], equivalent to the post-2021 behavior (Rust 1.51+)
+/// * replace `for ... in array.into_iter() {` with `for ... in array {`,
+///   equivalent to the post-2021 behavior (Rust 1.53+)
+///
+/// ```rust,edition2018
+/// use std::array::IntoIter;
+///
+/// let array: [i32; 3] = [0; 3];
+///
+/// // This iterates by reference:
+/// for item in array.iter() {
+///     let x: &i32 = item;
+///     println!("{}", x);
+/// }
+///
+/// // This iterates by value:
+/// for item in IntoIter::new(array) {
+///     let x: i32 = item;
+///     println!("{}", x);
+/// }
+///
+/// // This iterates by value:
+/// for item in array {
+///     let x: i32 = item;
+///     println!("{}", x);
+/// }
+///
+/// // IntoIter can also start a chain.
+/// // This iterates by value:
+/// for item in IntoIter::new(array).enumerate() {
+///     let (i, x): (usize, i32) = item;
+///     println!("array[{}] = {}", i, x);
+/// }
+/// ```
+///
 /// [slice]: prim@slice
 /// [`Debug`]: fmt::Debug
 /// [`Hash`]: hash::Hash