about summary refs log tree commit diff
path: root/library/alloc/src/vec/mod.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-04-17 19:21:44 +0000
committerbors <bors@rust-lang.org>2024-04-17 19:21:44 +0000
commitd261b5308111fa95427fcfdfd53a8331dd44a2b6 (patch)
tree8a985a2ac605d07089841ac906e13601c9d06390 /library/alloc/src/vec/mod.rs
parent9776f647e67cadaa8c5f56a04c933c72fe27ccbc (diff)
parent7c3c2716ff46fb3276c16bdaea226e5a5abc08c0 (diff)
downloadrust-d261b5308111fa95427fcfdfd53a8331dd44a2b6.tar.gz
rust-d261b5308111fa95427fcfdfd53a8331dd44a2b6.zip
Auto merge of #3481 - RalfJung:rustup, r=RalfJung
Rustup
Diffstat (limited to 'library/alloc/src/vec/mod.rs')
-rw-r--r--library/alloc/src/vec/mod.rs26
1 files changed, 24 insertions, 2 deletions
diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs
index 465da39f184..1930be65bfb 100644
--- a/library/alloc/src/vec/mod.rs
+++ b/library/alloc/src/vec/mod.rs
@@ -2846,8 +2846,30 @@ impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> {
         crate::slice::to_vec(&**self, alloc)
     }
 
-    fn clone_from(&mut self, other: &Self) {
-        crate::slice::SpecCloneIntoVec::clone_into(other.as_slice(), self);
+    /// Overwrites the contents of `self` with a clone of the contents of `source`.
+    ///
+    /// This method is preferred over simply assigning `source.clone()` to `self`,
+    /// as it avoids reallocation if possible. Additionally, if the element type
+    /// `T` overrides `clone_from()`, this will reuse the resources of `self`'s
+    /// elements as well.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = vec![5, 6, 7];
+    /// let mut y = vec![8, 9, 10];
+    /// let yp: *const i32 = y.as_ptr();
+    ///
+    /// y.clone_from(&x);
+    ///
+    /// // The value is the same
+    /// assert_eq!(x, y);
+    ///
+    /// // And no reallocation occurred
+    /// assert_eq!(yp, y.as_ptr());
+    /// ```
+    fn clone_from(&mut self, source: &Self) {
+        crate::slice::SpecCloneIntoVec::clone_into(source.as_slice(), self);
     }
 }