Skip to content

Improve prose around as_slice example of IterMut #134619

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

Merged
merged 1 commit into from
Jan 10, 2025
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
42 changes: 20 additions & 22 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<'a, T> IntoIterator for &'a mut [T] {
/// // First, we need a slice to call the `iter` method on:
/// let slice = &[1, 2, 3];
///
/// // Then we call `iter` on the slice to get the `Iter` struct,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually correct as-is. Given that it's documentation on the Iter type itself, there shouldn't be any confusion here. Likewise throughout.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is certainly correct, but later prose talks about calling the X method "of the iterator", without an iterator having been mentioned before. If you think it is less clear without struct, perhaps we can use something like "the Iter struct (which is an iterator)" or "the Iter struct (which implements the Iterator trait)"? What do you think?

/// // Then we call `iter` on the slice to get the `Iter` iterator,
/// // and iterate over it:
/// for element in slice.iter() {
/// println!("{element}");
Expand Down Expand Up @@ -107,24 +107,20 @@ impl<'a, T> Iter<'a, T> {

/// Views the underlying data as a subslice of the original data.
///
/// This has the same lifetime as the original slice, and so the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT this is just normal borrowing rules and does not seem especially relevant here.

/// iterator can continue to be used while this exists.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// // First, we need a slice to call the `iter` method on:
/// // struct (`&[usize]` here):
/// let slice = &[1, 2, 3];
///
/// // Then we call `iter` on the slice to get the `Iter` struct:
/// // Then we call `iter` on the slice to get the `Iter` iterator:
/// let mut iter = slice.iter();
/// // Here `as_slice` still returns the whole slice, so this prints "[1, 2, 3]":
/// println!("{:?}", iter.as_slice());
///
/// // Now, we call the `next` method to remove the first element of the iterator:
/// // Now, we call the `next` method to remove the first element from the iterator:
/// iter.next();
/// // Here the iterator does not contain the first element of the slice any more,
/// // so `as_slice` only returns the last two elements of the slice,
Expand Down Expand Up @@ -181,7 +177,7 @@ impl<T> AsRef<[T]> for Iter<'_, T> {
/// // First, we need a slice to call the `iter_mut` method on:
/// let slice = &mut [1, 2, 3];
///
/// // Then we call `iter_mut` on the slice to get the `IterMut` struct,
/// // Then we call `iter_mut` on the slice to get the `IterMut` iterator,
/// // iterate over it and increment each element value:
/// for element in slice.iter_mut() {
/// *element += 1;
Expand Down Expand Up @@ -286,25 +282,30 @@ impl<'a, T> IterMut<'a, T> {

/// Views the underlying data as a subslice of the original data.
///
/// To avoid creating `&mut [T]` references that alias, the returned slice
/// borrows its lifetime from the iterator the method is applied on.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let mut slice: &mut [usize] = &mut [1, 2, 3];
/// // First, we need a slice to call the `iter_mut` method on:
/// let slice = &mut [1, 2, 3];
///
/// // First, we get the iterator:
/// // Then we call `iter_mut` on the slice to get the `IterMut` iterator:
/// let mut iter = slice.iter_mut();
/// // So if we check what the `as_slice` method returns here, we have "[1, 2, 3]":
/// assert_eq!(iter.as_slice(), &[1, 2, 3]);
/// // Here `as_slice` still returns the whole slice, so this prints "[1, 2, 3]":
/// println!("{:?}", iter.as_slice());
///
/// // Next, we move to the second element of the slice:
/// iter.next();
/// // Now `as_slice` returns "[2, 3]":
/// assert_eq!(iter.as_slice(), &[2, 3]);
/// // Now, we call the `next` method to remove the first element from the iterator
/// // and increment its value:
/// *iter.next().unwrap() += 1;
/// // Here the iterator does not contain the first element of the slice any more,
/// // so `as_slice` only returns the last two elements of the slice,
/// // and so this prints "[2, 3]":
/// println!("{:?}", iter.as_slice());
///
/// // The underlying slice still contains three elements, but its first element
/// // was increased by 1, so this prints "[2, 2, 3]":
/// println!("{:?}", slice);
/// ```
#[must_use]
#[stable(feature = "slice_iter_mut_as_slice", since = "1.53.0")]
Expand All @@ -315,9 +316,6 @@ impl<'a, T> IterMut<'a, T> {

/// Views the underlying data as a mutable subslice of the original data.
///
/// To avoid creating `&mut [T]` references that alias, the returned slice
/// borrows its lifetime from the iterator the method is applied on.
///
/// # Examples
///
/// Basic usage:
Expand Down
Loading