@@ -91,6 +91,24 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
91
91
/// strong `Arc` pointers from parent nodes to children, and [`Weak`][weak]
92
92
/// pointers from children back to their parents.
93
93
///
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
+ ///
94
112
/// ## `Deref` behavior
95
113
///
96
114
/// `Arc<T>` automatically dereferences to `T` (via the [`Deref`][deref] trait),
@@ -138,7 +156,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
138
156
/// let five = Arc::new(5);
139
157
///
140
158
/// for _ in 0..10 {
141
- /// let five = five. clone();
159
+ /// let five = Arc:: clone(&five );
142
160
///
143
161
/// thread::spawn(move || {
144
162
/// println!("{:?}", five);
@@ -158,7 +176,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
158
176
/// let val = Arc::new(AtomicUsize::new(5));
159
177
///
160
178
/// for _ in 0..10 {
161
- /// let val = val. clone();
179
+ /// let val = Arc:: clone(&val );
162
180
///
163
181
/// thread::spawn(move || {
164
182
/// let v = val.fetch_add(1, Ordering::SeqCst);
@@ -282,7 +300,7 @@ impl<T> Arc<T> {
282
300
/// assert_eq!(Arc::try_unwrap(x), Ok(3));
283
301
///
284
302
/// let x = Arc::new(4);
285
- /// let _y = x. clone();
303
+ /// let _y = Arc:: clone(&x );
286
304
/// assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4);
287
305
/// ```
288
306
#[ inline]
@@ -451,7 +469,7 @@ impl<T: ?Sized> Arc<T> {
451
469
/// use std::sync::Arc;
452
470
///
453
471
/// let five = Arc::new(5);
454
- /// let _also_five = five. clone();
472
+ /// let _also_five = Arc:: clone(&five );
455
473
///
456
474
/// // This assertion is deterministic because we haven't shared
457
475
/// // the `Arc` between threads.
@@ -499,7 +517,7 @@ impl<T: ?Sized> Arc<T> {
499
517
/// use std::sync::Arc;
500
518
///
501
519
/// let five = Arc::new(5);
502
- /// let same_five = five. clone();
520
+ /// let same_five = Arc:: clone(&five );
503
521
/// let other_five = Arc::new(5);
504
522
///
505
523
/// assert!(Arc::ptr_eq(&five, &same_five));
@@ -524,7 +542,7 @@ impl<T: ?Sized> Clone for Arc<T> {
524
542
///
525
543
/// let five = Arc::new(5);
526
544
///
527
- /// five. clone();
545
+ /// Arc:: clone(&five );
528
546
/// ```
529
547
#[ inline]
530
548
fn clone ( & self ) -> Arc < T > {
@@ -591,7 +609,7 @@ impl<T: Clone> Arc<T> {
591
609
/// let mut data = Arc::new(5);
592
610
///
593
611
/// *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
595
613
/// *Arc::make_mut(&mut data) += 1; // Clones inner data
596
614
/// *Arc::make_mut(&mut data) += 1; // Won't clone anything
597
615
/// *Arc::make_mut(&mut other_data) *= 2; // Won't clone anything
@@ -679,7 +697,7 @@ impl<T: ?Sized> Arc<T> {
679
697
/// *Arc::get_mut(&mut x).unwrap() = 4;
680
698
/// assert_eq!(*x, 4);
681
699
///
682
- /// let _y = x. clone();
700
+ /// let _y = Arc:: clone(&x );
683
701
/// assert!(Arc::get_mut(&mut x).is_none());
684
702
/// ```
685
703
#[ inline]
@@ -751,7 +769,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc<T> {
751
769
/// }
752
770
///
753
771
/// let foo = Arc::new(Foo);
754
- /// let foo2 = foo. clone();
772
+ /// let foo2 = Arc:: clone(&foo );
755
773
///
756
774
/// drop(foo); // Doesn't print anything
757
775
/// drop(foo2); // Prints "dropped!"
@@ -903,11 +921,11 @@ impl<T: ?Sized> Clone for Weak<T> {
903
921
/// # Examples
904
922
///
905
923
/// ```
906
- /// use std::sync::Arc;
924
+ /// use std::sync::{ Arc, Weak} ;
907
925
///
908
926
/// let weak_five = Arc::downgrade(&Arc::new(5));
909
927
///
910
- /// weak_five. clone();
928
+ /// Weak:: clone(&weak_five );
911
929
/// ```
912
930
#[ inline]
913
931
fn clone ( & self ) -> Weak < T > {
@@ -956,7 +974,7 @@ impl<T: ?Sized> Drop for Weak<T> {
956
974
/// # Examples
957
975
///
958
976
/// ```
959
- /// use std::sync::Arc;
977
+ /// use std::sync::{ Arc, Weak} ;
960
978
///
961
979
/// struct Foo;
962
980
///
@@ -968,7 +986,7 @@ impl<T: ?Sized> Drop for Weak<T> {
968
986
///
969
987
/// let foo = Arc::new(Foo);
970
988
/// let weak_foo = Arc::downgrade(&foo);
971
- /// let other_weak_foo = weak_foo. clone();
989
+ /// let other_weak_foo = Weak:: clone(&weak_foo );
972
990
///
973
991
/// drop(weak_foo); // Doesn't print anything
974
992
/// drop(foo); // Prints "dropped!"
0 commit comments