Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Point out that behavior might be switched on 2015 and 2018 too one day #84590

Merged
merged 3 commits into from
Apr 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions library/std/src/primitive_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,10 @@ mod prim_pointer {}
/// # Editions
///
/// Prior to Rust 1.53, arrays did not implement `IntoIterator` by value, so the method call
/// `array.into_iter()` auto-referenced into a slice iterator. That behavior is preserved in the
/// 2015 and 2018 editions of Rust for compatability, ignoring `IntoIterator` by value.
/// `array.into_iter()` auto-referenced into a slice iterator. Right now, the old behavior
/// is preserved in the 2015 and 2018 editions of Rust for compatibility, ignoring
/// `IntoIterator` by value. In the future, the behavior on the 2015 and 2018 edition
/// might be made consistent to the behavior of later editions.
///
#[cfg_attr(bootstrap, doc = "```rust,edition2018,ignore")]
#[cfg_attr(not(bootstrap), doc = "```rust,edition2018")]
Expand Down Expand Up @@ -601,6 +603,49 @@ 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+)
///
#[cfg_attr(bootstrap, doc = "```rust,edition2018,ignore")]
#[cfg_attr(not(bootstrap), doc = "```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
Expand Down