Skip to content

Commit 4eca247

Browse files
authored
Add explanation for (copy, clone)_from_slice
It elaborates over why we have to slice the 4-size src to 2-size (same as dst).
1 parent 4b17d31 commit 4eca247

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/libcore/slice/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,10 @@ impl<T> [T] {
15411541
/// let src = [1, 2, 3, 4];
15421542
/// let mut dst = [0, 0];
15431543
///
1544+
/// // Note: the slices must be the same length, so
1545+
/// // you can slice the source to be the same size.
1546+
/// // Here we slice the source, four elements, to two, the same size
1547+
/// // as the destination slice. It *will* panic if we don't do this.
15441548
/// dst.clone_from_slice(&src[2..]);
15451549
///
15461550
/// assert_eq!(src, [1, 2, 3, 4]);
@@ -1607,6 +1611,10 @@ impl<T> [T] {
16071611
/// let src = [1, 2, 3, 4];
16081612
/// let mut dst = [0, 0];
16091613
///
1614+
/// // Note: the slices must be the same length, so
1615+
/// // you can slice the source to be the same size.
1616+
/// // Here we slice the source, four elements, to two, the same size
1617+
/// // as the destination slice. It *will* panic if we don't do this.
16101618
/// dst.copy_from_slice(&src[2..]);
16111619
///
16121620
/// assert_eq!(src, [1, 2, 3, 4]);

0 commit comments

Comments
 (0)