Explain fully qualified syntax for Rc
and Arc
This commit is contained in:
parent
31ee872db5
commit
bd7cbaecd3
2 changed files with 32 additions and 9 deletions
|
@ -11,7 +11,7 @@
|
||||||
//! is no exception: you cannot generally obtain a mutable reference to
|
//! is no exception: you cannot generally obtain a mutable reference to
|
||||||
//! something inside an [`Rc`]. If you need mutability, put a [`Cell`]
|
//! something inside an [`Rc`]. If you need mutability, put a [`Cell`]
|
||||||
//! or [`RefCell`] inside the [`Rc`]; see [an example of mutability
|
//! or [`RefCell`] inside the [`Rc`]; see [an example of mutability
|
||||||
//! inside an Rc][mutability].
|
//! inside an `Rc`][mutability].
|
||||||
//!
|
//!
|
||||||
//! [`Rc`] uses non-atomic reference counting. This means that overhead is very
|
//! [`Rc`] uses non-atomic reference counting. This means that overhead is very
|
||||||
//! low, but an [`Rc`] cannot be sent between threads, and consequently [`Rc`]
|
//! low, but an [`Rc`] cannot be sent between threads, and consequently [`Rc`]
|
||||||
|
@ -35,15 +35,26 @@
|
||||||
//! `Rc<T>` automatically dereferences to `T` (via the [`Deref`] trait),
|
//! `Rc<T>` automatically dereferences to `T` (via the [`Deref`] trait),
|
||||||
//! so you can call `T`'s methods on a value of type [`Rc<T>`][`Rc`]. To avoid name
|
//! so you can call `T`'s methods on a value of type [`Rc<T>`][`Rc`]. To avoid name
|
||||||
//! clashes with `T`'s methods, the methods of [`Rc<T>`][`Rc`] itself are associated
|
//! clashes with `T`'s methods, the methods of [`Rc<T>`][`Rc`] itself are associated
|
||||||
//! functions, called using function-like syntax:
|
//! functions, called using [fully qualified syntax]:
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
//! use std::rc::Rc;
|
//! use std::rc::Rc;
|
||||||
//! let my_rc = Rc::new(());
|
|
||||||
//!
|
//!
|
||||||
|
//! let my_rc = Rc::new(());
|
||||||
//! Rc::downgrade(&my_rc);
|
//! Rc::downgrade(&my_rc);
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
//! `Rc<T>`'s implementations of traits like `Clone` should also be called using
|
||||||
|
//! fully qualified syntax to avoid confusion as to whether the *reference* is being
|
||||||
|
//! cloned or the *backing data* (`T`) is being cloned:
|
||||||
|
//!
|
||||||
|
//! ```
|
||||||
|
//! use std::rc::Rc;
|
||||||
|
//!
|
||||||
|
//! let my_rc = Rc::new(());
|
||||||
|
//! let your_rc = Rc::clone(&my_rc);
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
//! [`Weak<T>`][`Weak`] does not auto-dereference to `T`, because the inner value may have
|
//! [`Weak<T>`][`Weak`] does not auto-dereference to `T`, because the inner value may have
|
||||||
//! already been dropped.
|
//! already been dropped.
|
||||||
//!
|
//!
|
||||||
|
@ -54,6 +65,7 @@
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
//! use std::rc::Rc;
|
//! use std::rc::Rc;
|
||||||
|
//!
|
||||||
//! let foo = Rc::new(vec![1.0, 2.0, 3.0]);
|
//! let foo = Rc::new(vec![1.0, 2.0, 3.0]);
|
||||||
//! // The two syntaxes below are equivalent.
|
//! // The two syntaxes below are equivalent.
|
||||||
//! let a = foo.clone();
|
//! let a = foo.clone();
|
||||||
|
@ -218,7 +230,7 @@
|
||||||
//! [`Cell`]: core::cell::Cell
|
//! [`Cell`]: core::cell::Cell
|
||||||
//! [`RefCell`]: core::cell::RefCell
|
//! [`RefCell`]: core::cell::RefCell
|
||||||
//! [send]: core::marker::Send
|
//! [send]: core::marker::Send
|
||||||
//! [arc]: ../../std/sync/struct.Arc.html
|
//! [arc]: alloc::sync::Arc
|
||||||
//! [`Deref`]: core::ops::Deref
|
//! [`Deref`]: core::ops::Deref
|
||||||
//! [downgrade]: Rc::downgrade
|
//! [downgrade]: Rc::downgrade
|
||||||
//! [upgrade]: Weak::upgrade
|
//! [upgrade]: Weak::upgrade
|
||||||
|
@ -272,10 +284,9 @@ struct RcBox<T: ?Sized> {
|
||||||
///
|
///
|
||||||
/// The inherent methods of `Rc` are all associated functions, which means
|
/// The inherent methods of `Rc` are all associated functions, which means
|
||||||
/// that you have to call them as e.g., [`Rc::get_mut(&mut value)`][get_mut] instead of
|
/// that you have to call them as e.g., [`Rc::get_mut(&mut value)`][get_mut] instead of
|
||||||
/// `value.get_mut()`. This avoids conflicts with methods of the inner
|
/// `value.get_mut()`. This avoids conflicts with methods of the inner type `T`.
|
||||||
/// type `T`.
|
|
||||||
///
|
///
|
||||||
/// [get_mut]: #method.get_mut
|
/// [get_mut]: Rc::get_mut
|
||||||
#[cfg_attr(not(test), rustc_diagnostic_item = "Rc")]
|
#[cfg_attr(not(test), rustc_diagnostic_item = "Rc")]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub struct Rc<T: ?Sized> {
|
pub struct Rc<T: ?Sized> {
|
||||||
|
|
|
@ -129,15 +129,26 @@ macro_rules! acquire {
|
||||||
/// `Arc<T>` automatically dereferences to `T` (via the [`Deref`][deref] trait),
|
/// `Arc<T>` automatically dereferences to `T` (via the [`Deref`][deref] trait),
|
||||||
/// so you can call `T`'s methods on a value of type `Arc<T>`. To avoid name
|
/// so you can call `T`'s methods on a value of type `Arc<T>`. To avoid name
|
||||||
/// clashes with `T`'s methods, the methods of `Arc<T>` itself are associated
|
/// clashes with `T`'s methods, the methods of `Arc<T>` itself are associated
|
||||||
/// functions, called using function-like syntax:
|
/// functions, called using [fully qualified syntax]:
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use std::sync::Arc;
|
/// use std::sync::Arc;
|
||||||
/// let my_arc = Arc::new(());
|
|
||||||
///
|
///
|
||||||
|
/// let my_arc = Arc::new(());
|
||||||
/// Arc::downgrade(&my_arc);
|
/// Arc::downgrade(&my_arc);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
/// `Arc<T>`'s implementations of traits like `Clone` should also be called using
|
||||||
|
/// fully qualified syntax to avoid confusion as to whether the *reference* is being
|
||||||
|
/// cloned or the *backing data* (`T`) is being cloned:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::sync::Arc;
|
||||||
|
///
|
||||||
|
/// let my_arc = Arc::new(());
|
||||||
|
/// let your_arc = Arc::clone(&my_arc);
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
/// [`Weak<T>`][Weak] does not auto-dereference to `T`, because the inner value may have
|
/// [`Weak<T>`][Weak] does not auto-dereference to `T`, because the inner value may have
|
||||||
/// already been dropped.
|
/// already been dropped.
|
||||||
///
|
///
|
||||||
|
@ -154,6 +165,7 @@ macro_rules! acquire {
|
||||||
/// [`RefCell<T>`]: core::cell::RefCell
|
/// [`RefCell<T>`]: core::cell::RefCell
|
||||||
/// [`std::sync`]: ../../std/sync/index.html
|
/// [`std::sync`]: ../../std/sync/index.html
|
||||||
/// [`Arc::clone(&from)`]: Arc::clone
|
/// [`Arc::clone(&from)`]: Arc::clone
|
||||||
|
/// [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
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue