-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
doc: improve Drain examples #30272
doc: improve Drain examples #30272
Conversation
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
@@ -1015,8 +1015,7 @@ impl String { | |||
} | |||
|
|||
/// Create a draining iterator that removes the specified range in the string | |||
/// and yields the removed chars from start to end. The element range is | |||
/// removed even if the iterator is not consumed until the end. |
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.
The information about removal is good I think. You might otherwise think that only elements extracted from the iterator are removed from the String or Vec, or VecDeque.
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.
@bluss it's only the extracted elements that are removed
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.
No it's not, I implemented this for Vec and String.
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.
Not sure I understand you. Following does not panic:
let mut v = vec![1, 2, 3];
assert_eq!(vec![3], v.drain(2..).collect::<Vec<_>>());
assert_eq!(vec![1, 2], v);
v.drain(..);
assert!(v.is_empty());
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.
ok, I mean that:
let mut v = vec![1, 2, 3];
v.drain(..).take(1).collect::<Vec<_>>();
All elements in the range are removed even if only 1 element is taken ("extracted") from the iterator.
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.
Now I understand. But that is somewhat orthogonal isn't it... not worth mentioning in the summary. Also, the examples I added also demonstrate the fact you mention.
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.
It should be mentioned somewhere in the docs for the method IMO. It's something users have already wondered about, always best to say it explicitly.
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.
alright
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.
I re-instated the info @bluss
Thanks! @bors r+ |
📌 Commit 46e2296 has been approved by |
Second sentence actually repeats info from first sentence. "from start to end" also feels like it adds nothing. I also extended Vec::drain example.
⛄ The build was interrupted to prioritize another pull request. |
⚡ Previous build results for auto-linux-32-nopt-t, auto-linux-32-opt, auto-linux-64-debug-opt, auto-linux-64-nopt-t, auto-linux-64-opt, auto-linux-64-x-android-t, auto-linux-cross-opt, auto-linux-musl-64-opt, auto-mac-32-opt, auto-mac-64-nopt-t, auto-mac-64-opt, auto-win-gnu-32-nopt-t, auto-win-gnu-64-opt, auto-win-msvc-32-opt are reusable. Rebuilding only auto-win-gnu-32-opt, auto-win-gnu-64-nopt-t, auto-win-msvc-64-opt... |
Second sentence actually repeats info from first sentence. "from start to end" also feels like it adds nothing.
I also extended Vec::drain example.