@@ -91,6 +91,24 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
9191/// strong `Arc` pointers from parent nodes to children, and [`Weak`][weak]
9292/// pointers from children back to their parents.
9393///
94+ /// # Cloning references
95+ ///
96+ /// Creating a new reference from an existing reference counted pointer is done using the
97+ /// `Clone` trait implemented for [`Arc<T>`][`arc`] and [`Weak<T>`][`weak`].
98+ ///
99+ /// ```
100+ /// use std::sync::Arc;
101+ /// let foo = Arc::new(vec![1.0, 2.0, 3.0]);
102+ /// // The two syntaxes below are equivalent.
103+ /// let a = foo.clone();
104+ /// let b = Arc::clone(&foo);
105+ /// // a and b both point to the same memory location as foo.
106+ /// ```
107+ ///
108+ /// The `Arc::clone(&from)` syntax is the most idiomatic because it conveys more explicitly
109+ /// the meaning of the code. In the example above, this syntax makes it easier to see that
110+ /// this code is creating a new reference rather than copying the whole content of foo.
111+ ///
94112/// ## `Deref` behavior
95113///
96114/// `Arc<T>` automatically dereferences to `T` (via the [`Deref`][deref] trait),
@@ -138,7 +156,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
138156/// let five = Arc::new(5);
139157///
140158/// for _ in 0..10 {
141- /// let five = five. clone();
159+ /// let five = Arc:: clone(&five );
142160///
143161/// thread::spawn(move || {
144162/// println!("{:?}", five);
@@ -158,7 +176,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
158176/// let val = Arc::new(AtomicUsize::new(5));
159177///
160178/// for _ in 0..10 {
161- /// let val = val. clone();
179+ /// let val = Arc:: clone(&val );
162180///
163181/// thread::spawn(move || {
164182/// let v = val.fetch_add(1, Ordering::SeqCst);
@@ -282,7 +300,7 @@ impl<T> Arc<T> {
282300 /// assert_eq!(Arc::try_unwrap(x), Ok(3));
283301 ///
284302 /// let x = Arc::new(4);
285- /// let _y = x. clone();
303+ /// let _y = Arc:: clone(&x );
286304 /// assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4);
287305 /// ```
288306 #[ inline]
@@ -451,7 +469,7 @@ impl<T: ?Sized> Arc<T> {
451469 /// use std::sync::Arc;
452470 ///
453471 /// let five = Arc::new(5);
454- /// let _also_five = five. clone();
472+ /// let _also_five = Arc:: clone(&five );
455473 ///
456474 /// // This assertion is deterministic because we haven't shared
457475 /// // the `Arc` between threads.
@@ -499,7 +517,7 @@ impl<T: ?Sized> Arc<T> {
499517 /// use std::sync::Arc;
500518 ///
501519 /// let five = Arc::new(5);
502- /// let same_five = five. clone();
520+ /// let same_five = Arc:: clone(&five );
503521 /// let other_five = Arc::new(5);
504522 ///
505523 /// assert!(Arc::ptr_eq(&five, &same_five));
@@ -524,7 +542,7 @@ impl<T: ?Sized> Clone for Arc<T> {
524542 ///
525543 /// let five = Arc::new(5);
526544 ///
527- /// five. clone();
545+ /// Arc:: clone(&five );
528546 /// ```
529547 #[ inline]
530548 fn clone ( & self ) -> Arc < T > {
@@ -591,7 +609,7 @@ impl<T: Clone> Arc<T> {
591609 /// let mut data = Arc::new(5);
592610 ///
593611 /// *Arc::make_mut(&mut data) += 1; // Won't clone anything
594- /// let mut other_data = data. clone(); // Won't clone inner data
612+ /// let mut other_data = Arc:: clone(&data); // Won't clone inner data
595613 /// *Arc::make_mut(&mut data) += 1; // Clones inner data
596614 /// *Arc::make_mut(&mut data) += 1; // Won't clone anything
597615 /// *Arc::make_mut(&mut other_data) *= 2; // Won't clone anything
@@ -679,7 +697,7 @@ impl<T: ?Sized> Arc<T> {
679697 /// *Arc::get_mut(&mut x).unwrap() = 4;
680698 /// assert_eq!(*x, 4);
681699 ///
682- /// let _y = x. clone();
700+ /// let _y = Arc:: clone(&x );
683701 /// assert!(Arc::get_mut(&mut x).is_none());
684702 /// ```
685703 #[ inline]
@@ -751,7 +769,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc<T> {
751769 /// }
752770 ///
753771 /// let foo = Arc::new(Foo);
754- /// let foo2 = foo. clone();
772+ /// let foo2 = Arc:: clone(&foo );
755773 ///
756774 /// drop(foo); // Doesn't print anything
757775 /// drop(foo2); // Prints "dropped!"
@@ -903,11 +921,11 @@ impl<T: ?Sized> Clone for Weak<T> {
903921 /// # Examples
904922 ///
905923 /// ```
906- /// use std::sync::Arc;
924+ /// use std::sync::{ Arc, Weak} ;
907925 ///
908926 /// let weak_five = Arc::downgrade(&Arc::new(5));
909927 ///
910- /// weak_five. clone();
928+ /// Weak:: clone(&weak_five );
911929 /// ```
912930 #[ inline]
913931 fn clone ( & self ) -> Weak < T > {
@@ -956,7 +974,7 @@ impl<T: ?Sized> Drop for Weak<T> {
956974 /// # Examples
957975 ///
958976 /// ```
959- /// use std::sync::Arc;
977+ /// use std::sync::{ Arc, Weak} ;
960978 ///
961979 /// struct Foo;
962980 ///
@@ -968,7 +986,7 @@ impl<T: ?Sized> Drop for Weak<T> {
968986 ///
969987 /// let foo = Arc::new(Foo);
970988 /// let weak_foo = Arc::downgrade(&foo);
971- /// let other_weak_foo = weak_foo. clone();
989+ /// let other_weak_foo = Weak:: clone(&weak_foo );
972990 ///
973991 /// drop(weak_foo); // Doesn't print anything
974992 /// drop(foo); // Prints "dropped!"
0 commit comments