Skip to content

Commit a384a58

Browse files
Rollup merge of #76138 - camelid:rc-fully-qualified-syntax, r=steveklabnik
Explain fully qualified syntax for `Rc` and `Arc` Also cleaned up some other small things. @rustbot modify labels: T-doc
2 parents 2168210 + 4e30e10 commit a384a58

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

library/alloc/src/rc.rs

+21-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,29 @@
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` may also be called using
48+
//! fully qualified syntax. Some people prefer to use fully qualified syntax,
49+
//! while others prefer using method-call syntax.
50+
//!
51+
//! ```
52+
//! use std::rc::Rc;
53+
//!
54+
//! let rc = Rc::new(());
55+
//! // Method-call syntax
56+
//! let rc2 = rc.clone();
57+
//! // Fully qualified syntax
58+
//! let rc3 = Rc::clone(&rc);
59+
//! ```
60+
//!
4761
//! [`Weak<T>`][`Weak`] does not auto-dereference to `T`, because the inner value may have
4862
//! already been dropped.
4963
//!
@@ -54,6 +68,7 @@
5468
//!
5569
//! ```
5670
//! use std::rc::Rc;
71+
//!
5772
//! let foo = Rc::new(vec![1.0, 2.0, 3.0]);
5873
//! // The two syntaxes below are equivalent.
5974
//! let a = foo.clone();
@@ -218,7 +233,7 @@
218233
//! [`Cell`]: core::cell::Cell
219234
//! [`RefCell`]: core::cell::RefCell
220235
//! [send]: core::marker::Send
221-
//! [arc]: ../../std/sync/struct.Arc.html
236+
//! [arc]: crate::sync::Arc
222237
//! [`Deref`]: core::ops::Deref
223238
//! [downgrade]: Rc::downgrade
224239
//! [upgrade]: Weak::upgrade
@@ -272,10 +287,9 @@ struct RcBox<T: ?Sized> {
272287
///
273288
/// The inherent methods of `Rc` are all associated functions, which means
274289
/// 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`.
290+
/// `value.get_mut()`. This avoids conflicts with methods of the inner type `T`.
277291
///
278-
/// [get_mut]: #method.get_mut
292+
/// [get_mut]: Rc::get_mut
279293
#[cfg_attr(not(test), rustc_diagnostic_item = "Rc")]
280294
#[stable(feature = "rust1", since = "1.0.0")]
281295
pub struct Rc<T: ?Sized> {

library/alloc/src/sync.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,29 @@ 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` may also be called using
142+
/// fully qualified syntax. Some people prefer to use fully qualified syntax,
143+
/// while others prefer using method-call syntax.
144+
///
145+
/// ```
146+
/// use std::sync::Arc;
147+
///
148+
/// let arc = Arc::new(());
149+
/// // Method-call syntax
150+
/// let arc2 = arc.clone();
151+
/// // Fully qualified syntax
152+
/// let arc3 = Arc::clone(&arc);
153+
/// ```
154+
///
141155
/// [`Weak<T>`][Weak] does not auto-dereference to `T`, because the inner value may have
142156
/// already been dropped.
143157
///
@@ -154,6 +168,7 @@ macro_rules! acquire {
154168
/// [`RefCell<T>`]: core::cell::RefCell
155169
/// [`std::sync`]: ../../std/sync/index.html
156170
/// [`Arc::clone(&from)`]: Arc::clone
171+
/// [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
157172
///
158173
/// # Examples
159174
///

0 commit comments

Comments
 (0)