Skip to content

Better docs for copy_from_slice & clone_from_slice #51701

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 5 commits into from
Jul 11, 2018
Merged
Changes from 3 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
6 changes: 6 additions & 0 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,9 @@ impl<T> [T] {
/// let src = [1, 2, 3, 4];
/// let mut dst = [0, 0];
///
/// // Note: the slices must be the same length, so you can slice the
/// // source to be the same size. Here we slice the source, four elements,
Copy link
Member

Choose a reason for hiding this comment

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

This all make sense to me, though I'm a little confused by this phrase:

so you can slice the source to be the same size

...because in some scenarios, you'll need to slice the destination to match the length of the source.

I think this would be good enough:

Note: The slices must be the same length, otherwise this method will panic. Here we slice the source, four elements, to two, the same size as the destination slice.

What do you think?

Copy link
Contributor Author

@anirudhb anirudhb Jul 9, 2018

Choose a reason for hiding this comment

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

I think changing it to "so you can slice the source or the destination to be the same size." would be fine.
Edit: added in most recent commit

/// // to two, the same size as the destination slice. It *will* panic if we don't do this.
/// dst.clone_from_slice(&src[2..]);
///
/// assert_eq!(src, [1, 2, 3, 4]);
Expand Down Expand Up @@ -1607,6 +1610,9 @@ impl<T> [T] {
/// let src = [1, 2, 3, 4];
/// let mut dst = [0, 0];
///
/// // Note: the slices must be the same length, so you can slice the
/// // source to be the same size. Here we slice the source, four elements,
/// // to two, the same size as the destination slice. It *will* panic if we don't do this.
/// dst.copy_from_slice(&src[2..]);
///
/// assert_eq!(src, [1, 2, 3, 4]);
Expand Down