]> git.lizzy.rs Git - rust.git/commitdiff
Add explanation for (copy, clone)_from_slice
authorAnirudh Balaji <anirudhb@users.noreply.github.com>
Fri, 22 Jun 2018 07:20:51 +0000 (00:20 -0700)
committerGitHub <noreply@github.com>
Fri, 22 Jun 2018 07:20:51 +0000 (00:20 -0700)
It elaborates over why we have to slice the 4-size src to 2-size (same as dst).

src/libcore/slice/mod.rs

index 6f4c130d8f318b1eb1114ad6b7b7591e8f98c2aa..01db55a00f647ecd6a56e3add5c2cc92364affc9 100644 (file)
@@ -1541,6 +1541,10 @@ pub fn rotate_right(&mut self, k: usize) {
     /// 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.clone_from_slice(&src[2..]);
     ///
     /// assert_eq!(src, [1, 2, 3, 4]);
@@ -1607,6 +1611,10 @@ pub fn clone_from_slice(&mut self, src: &[T]) where T: Clone {
     /// 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]);