Skip to content

Commit bd7cbae

Browse files
committed
Explain fully qualified syntax for Rc and Arc
1 parent 31ee872 commit bd7cbae

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

library/alloc/src/rc.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! is no exception: you cannot generally obtain a mutable reference to
1212
//! something inside an [`Rc`]. If you need mutability, put a [`Cell`]
1313
//! or [`RefCell`] inside the [`Rc`]; see [an example of mutability
14-
//! inside an Rc][mutability].
14+
//! inside an `Rc`][mutability].
1515
//!
1616
//! [`Rc`] uses non-atomic reference counting. This means that overhead is very
1717
//! low, but an [`Rc`] cannot be sent between threads, and consequently [`Rc`]
@@ -35,15 +35,26 @@
3535
//! `Rc<T>` automatically dereferences to `T` (via the [`Deref`] trait),
3636
//! so you can call `T`'s methods on a value of type [`Rc<T>`][`Rc`]. To avoid name
3737
//! clashes with `T`'s methods, the methods of [`Rc<T>`][`Rc`] itself are associated
38-
//! functions, called using function-like syntax:
38+
//! functions, called using [fully qualified syntax]:
3939
//!
4040
//! ```
4141
//! use std::rc::Rc;
42-
//! let my_rc = Rc::new(());
4342
//!
43+
//! let my_rc = Rc::new(());
4444
//! Rc::downgrade(&my_rc);
4545
//! ```
4646
//!
47+
//! `Rc<T>`'s implementations of traits like `Clone` should also be called using
48+
//! fully qualified syntax to avoid confusion as to whether the *reference* is being
49+
//! cloned or the *backing data* (`T`) is being cloned:
50+
//!
51+
//! ```
52+
//! use std::rc::Rc;
53+
//!
54+
//! let my_rc = Rc::new(());
55+
//! let your_rc = Rc::clone(&my_rc);
56+
//! ```
57+
//!
4758
//! [`Weak<T>`][`Weak`] does not auto-dereference to `T`, because the inner value may have
4859
//! already been dropped.
4960
//!
@@ -54,6 +65,7 @@
5465
//!
5566
//! ```
5667
//! use std::rc::Rc;
68+
//!
5769
//! let foo = Rc::new(vec![1.0, 2.0, 3.0]);
5870
//! // The two syntaxes below are equivalent.
5971
//! let a = foo.clone();
@@ -218,7 +230,7 @@
218230
//! [`Cell`]: core::cell::Cell
219231
//! [`RefCell`]: core::cell::RefCell
220232
//! [send]: core::marker::Send
221-
//! [arc]: ../../std/sync/struct.Arc.html
233+
//! [arc]: alloc::sync::Arc
222234
//! [`Deref`]: core::ops::Deref
223235
//! [downgrade]: Rc::downgrade
224236
//! [upgrade]: Weak::upgrade
@@ -272,10 +284,9 @@ struct RcBox<T: ?Sized> {
272284
///
273285
/// The inherent methods of `Rc` are all associated functions, which means
274286
/// that you have to call them as e.g., [`Rc::get_mut(&mut value)`][get_mut] instead of
275-
/// `value.get_mut()`. This avoids conflicts with methods of the inner
276-
/// type `T`.
287+
/// `value.get_mut()`. This avoids conflicts with methods of the inner type `T`.
277288
///
278-
/// [get_mut]: #method.get_mut
289+
/// [get_mut]: Rc::get_mut
279290
#[cfg_attr(not(test), rustc_diagnostic_item = "Rc")]
280291
#[stable(feature = "rust1", since = "1.0.0")]
281292
pub struct Rc<T: ?Sized> {

library/alloc/src/sync.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,26 @@ macro_rules! acquire {
129129
/// `Arc<T>` automatically dereferences to `T` (via the [`Deref`][deref] trait),
130130
/// so you can call `T`'s methods on a value of type `Arc<T>`. To avoid name
131131
/// clashes with `T`'s methods, the methods of `Arc<T>` itself are associated
132-
/// functions, called using function-like syntax:
132+
/// functions, called using [fully qualified syntax]:
133133
///
134134
/// ```
135135
/// use std::sync::Arc;
136-
/// let my_arc = Arc::new(());
137136
///
137+
/// let my_arc = Arc::new(());
138138
/// Arc::downgrade(&my_arc);
139139
/// ```
140140
///
141+
/// `Arc<T>`'s implementations of traits like `Clone` should also be called using
142+
/// fully qualified syntax to avoid confusion as to whether the *reference* is being
143+
/// cloned or the *backing data* (`T`) is being cloned:
144+
///
145+
/// ```
146+
/// use std::sync::Arc;
147+
///
148+
/// let my_arc = Arc::new(());
149+
/// let your_arc = Arc::clone(&my_arc);
150+
/// ```
151+
///
141152
/// [`Weak<T>`][Weak] does not auto-dereference to `T`, because the inner value may have
142153
/// already been dropped.
143154
///
@@ -154,6 +165,7 @@ macro_rules! acquire {
154165
/// [`RefCell<T>`]: core::cell::RefCell
155166
/// [`std::sync`]: ../../std/sync/index.html
156167
/// [`Arc::clone(&from)`]: Arc::clone
168+
/// [fully qualified syntax]: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name
157169
///
158170
/// # Examples
159171
///

0 commit comments

Comments
 (0)