-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
/// // Then we call `iter` on the slice to get the `Iter` iterator, | ||
/// // and iterate over it: | ||
/// for element in slice.iter() { | ||
/// println!("{element}"); | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why remove this? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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; | ||
|
@@ -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")] | ||
|
@@ -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: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 "theIter
struct (which implements the Iterator trait)"? What do you think?