|
11 | 11 | //! is no exception: you cannot generally obtain a mutable reference to |
12 | 12 | //! something inside an [`Rc`]. If you need mutability, put a [`Cell`] |
13 | 13 | //! or [`RefCell`] inside the [`Rc`]; see [an example of mutability |
14 | | -//! inside an Rc][mutability]. |
| 14 | +//! inside an `Rc`][mutability]. |
15 | 15 | //! |
16 | 16 | //! [`Rc`] uses non-atomic reference counting. This means that overhead is very |
17 | 17 | //! low, but an [`Rc`] cannot be sent between threads, and consequently [`Rc`] |
|
35 | 35 | //! `Rc<T>` automatically dereferences to `T` (via the [`Deref`] trait), |
36 | 36 | //! so you can call `T`'s methods on a value of type [`Rc<T>`][`Rc`]. To avoid name |
37 | 37 | //! 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]: |
39 | 39 | //! |
40 | 40 | //! ``` |
41 | 41 | //! use std::rc::Rc; |
42 | | -//! let my_rc = Rc::new(()); |
43 | 42 | //! |
| 43 | +//! let my_rc = Rc::new(()); |
44 | 44 | //! Rc::downgrade(&my_rc); |
45 | 45 | //! ``` |
46 | 46 | //! |
| 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 | +//! |
47 | 58 | //! [`Weak<T>`][`Weak`] does not auto-dereference to `T`, because the inner value may have |
48 | 59 | //! already been dropped. |
49 | 60 | //! |
|
54 | 65 | //! |
55 | 66 | //! ``` |
56 | 67 | //! use std::rc::Rc; |
| 68 | +//! |
57 | 69 | //! let foo = Rc::new(vec![1.0, 2.0, 3.0]); |
58 | 70 | //! // The two syntaxes below are equivalent. |
59 | 71 | //! let a = foo.clone(); |
|
218 | 230 | //! [`Cell`]: core::cell::Cell |
219 | 231 | //! [`RefCell`]: core::cell::RefCell |
220 | 232 | //! [send]: core::marker::Send |
221 | | -//! [arc]: ../../std/sync/struct.Arc.html |
| 233 | +//! [arc]: alloc::sync::Arc |
222 | 234 | //! [`Deref`]: core::ops::Deref |
223 | 235 | //! [downgrade]: Rc::downgrade |
224 | 236 | //! [upgrade]: Weak::upgrade |
@@ -272,10 +284,9 @@ struct RcBox<T: ?Sized> { |
272 | 284 | /// |
273 | 285 | /// The inherent methods of `Rc` are all associated functions, which means |
274 | 286 | /// 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`. |
277 | 288 | /// |
278 | | -/// [get_mut]: #method.get_mut |
| 289 | +/// [get_mut]: Rc::get_mut |
279 | 290 | #[cfg_attr(not(test), rustc_diagnostic_item = "Rc")] |
280 | 291 | #[stable(feature = "rust1", since = "1.0.0")] |
281 | 292 | pub struct Rc<T: ?Sized> { |
|
0 commit comments