Skip to content

Commit f774bce

Browse files
committed
Auto merge of #43724 - lukaramu:std-ops-docs, r=QuietMisdreavus
Improve std::ops docs Fixes #29365. (This fixes all but one point from @steveklabnik's list, but that point was referring to examples of implementing range traits, but there are no range traits in std::ops.) The main changes are quite a bit of copyediting, adding more "real" examples for some of the traits, incorporating some guidance from the API docs, more linking (cross-docs and to the book & reference), cleaning up examples, moving things around, and so on. Refer to the commit messages for more details. Note: I decided to link to the second edition of the book since I think it's more appropriate now for the sections I linked, if this is not okay, please say so!
2 parents 0269acb + 6bdba82 commit f774bce

File tree

8 files changed

+615
-529
lines changed

8 files changed

+615
-529
lines changed

src/libcore/ops/arith.rs

+120-182
Large diffs are not rendered by default.

src/libcore/ops/bit.rs

+128-150
Large diffs are not rendered by default.

src/libcore/ops/deref.rs

+78-24
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,44 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
/// The `Deref` trait is used to specify the functionality of dereferencing
12-
/// operations, like `*v`.
11+
/// Used for immutable dereferencing operations, like `*v`.
1312
///
14-
/// `Deref` also enables ['`Deref` coercions'][coercions].
13+
/// In addition to being used for explicit dereferencing operations with the
14+
/// (unary) `*` operator in immutable contexts, `Deref` is also used implicitly
15+
/// by the compiler in many circumstances. This mechanism is called
16+
/// ['`Deref` coercion'][more]. In mutable contexts, [`DerefMut`] is used.
1517
///
16-
/// [coercions]: ../../book/first-edition/deref-coercions.html
18+
/// Implementing `Deref` for smart pointers makes accessing the data behind them
19+
/// convenient, which is why they implement `Deref`. On the other hand, the
20+
/// rules regarding `Deref` and [`DerefMut`] were designed specifically to
21+
/// accomodate smart pointers. Because of this, **`Deref` should only be
22+
/// implemented for smart pointers** to avoid confusion.
23+
///
24+
/// For similar reasons, **this trait should never fail**. Failure during
25+
/// dereferencing can be extremely confusing when `Deref` is invoked implicitly.
26+
///
27+
/// # More on `Deref` coercion
28+
///
29+
/// If `T` implements `Deref<Target = U>`, and `x` is a value of type `T`, then:
30+
/// * In immutable contexts, `*x` on non-pointer types is equivalent to
31+
/// `*Deref::deref(&x)`.
32+
/// * Values of type `&T` are coerced to values of type `&U`
33+
/// * `T` implicitly implements all the (immutable) methods of the type `U`.
34+
///
35+
/// For more details, visit [the chapter in *The Rust Programming Language*]
36+
/// [book] as well as the reference sections on [the dereference operator]
37+
/// [ref-deref-op], [the `Deref` trait][ref-deref-trait], and [type coercions].
38+
///
39+
/// [book]: ../../book/second-edition/ch15-02-deref.html
40+
/// [`DerefMut`]: trait.DerefMut.html
41+
/// [more]: #more-on-deref-coercion
42+
/// [ref-deref-op]: ../../reference/expressions.html#the-dereference-operator
43+
/// [ref-deref-trait]: ../../reference/the-deref-trait.html
44+
/// [type coercions]: ../../reference/type-coercions.html
1745
///
1846
/// # Examples
1947
///
20-
/// A struct with a single field which is accessible via dereferencing the
48+
/// A struct with a single field which is accessible by dereferencing the
2149
/// struct.
2250
///
2351
/// ```
@@ -35,19 +63,17 @@
3563
/// }
3664
/// }
3765
///
38-
/// fn main() {
39-
/// let x = DerefExample { value: 'a' };
40-
/// assert_eq!('a', *x);
41-
/// }
66+
/// let x = DerefExample { value: 'a' };
67+
/// assert_eq!('a', *x);
4268
/// ```
4369
#[lang = "deref"]
4470
#[stable(feature = "rust1", since = "1.0.0")]
4571
pub trait Deref {
46-
/// The resulting type after dereferencing
72+
/// The resulting type after dereferencing.
4773
#[stable(feature = "rust1", since = "1.0.0")]
4874
type Target: ?Sized;
4975

50-
/// The method called to dereference a value
76+
/// Dereferences the value.
5177
#[stable(feature = "rust1", since = "1.0.0")]
5278
fn deref(&self) -> &Self::Target;
5379
}
@@ -66,16 +92,46 @@ impl<'a, T: ?Sized> Deref for &'a mut T {
6692
fn deref(&self) -> &T { *self }
6793
}
6894

69-
/// The `DerefMut` trait is used to specify the functionality of dereferencing
70-
/// mutably like `*v = 1;`
71-
///
72-
/// `DerefMut` also enables ['`Deref` coercions'][coercions].
73-
///
74-
/// [coercions]: ../../book/first-edition/deref-coercions.html
95+
/// Used for mutable dereferencing operations, like in `*v = 1;`.
96+
///
97+
/// In addition to being used for explicit dereferencing operations with the
98+
/// (unary) `*` operator in mutable contexts, `DerefMut` is also used implicitly
99+
/// by the compiler in many circumstances. This mechanism is called
100+
/// ['`Deref` coercion'][more]. In immutable contexts, [`Deref`] is used.
101+
///
102+
/// Implementing `DerefMut` for smart pointers makes mutating the data behind
103+
/// them convenient, which is why they implement `DerefMut`. On the other hand,
104+
/// the rules regarding [`Deref`] and `DerefMut` were designed specifically to
105+
/// accomodate smart pointers. Because of this, **`DerefMut` should only be
106+
/// implemented for smart pointers** to avoid confusion.
107+
///
108+
/// For similar reasons, **this trait should never fail**. Failure during
109+
/// dereferencing can be extremely confusing when `DerefMut` is invoked
110+
/// implicitly.
111+
///
112+
/// # More on `Deref` coercion
113+
///
114+
/// If `T` implements `DerefMut<Target = U>`, and `x` is a value of type `T`,
115+
/// then:
116+
/// * In mutable contexts, `*x` on non-pointer types is equivalent to
117+
/// `*Deref::deref(&x)`.
118+
/// * Values of type `&mut T` are coerced to values of type `&mut U`
119+
/// * `T` implicitly implements all the (mutable) methods of the type `U`.
120+
///
121+
/// For more details, visit [the chapter in *The Rust Programming Language*]
122+
/// [book] as well as the reference sections on [the dereference operator]
123+
/// [ref-deref-op], [the `Deref` trait][ref-deref-trait], and [type coercions].
124+
///
125+
/// [book]: ../../book/second-edition/ch15-02-deref.html
126+
/// [`Deref`]: trait.Deref.html
127+
/// [more]: #more-on-deref-coercion
128+
/// [ref-deref-op]: ../../reference/expressions.html#the-dereference-operator
129+
/// [ref-deref-trait]: ../../reference/the-deref-trait.html
130+
/// [type coercions]: ../../reference/type-coercions.html
75131
///
76132
/// # Examples
77133
///
78-
/// A struct with a single field which is modifiable via dereferencing the
134+
/// A struct with a single field which is modifiable by dereferencing the
79135
/// struct.
80136
///
81137
/// ```
@@ -99,16 +155,14 @@ impl<'a, T: ?Sized> Deref for &'a mut T {
99155
/// }
100156
/// }
101157
///
102-
/// fn main() {
103-
/// let mut x = DerefMutExample { value: 'a' };
104-
/// *x = 'b';
105-
/// assert_eq!('b', *x);
106-
/// }
158+
/// let mut x = DerefMutExample { value: 'a' };
159+
/// *x = 'b';
160+
/// assert_eq!('b', *x);
107161
/// ```
108162
#[lang = "deref_mut"]
109163
#[stable(feature = "rust1", since = "1.0.0")]
110164
pub trait DerefMut: Deref {
111-
/// The method called to mutably dereference a value
165+
/// Mutably dereferences the value.
112166
#[stable(feature = "rust1", since = "1.0.0")]
113167
fn deref_mut(&mut self) -> &mut Self::Target;
114168
}

src/libcore/ops/drop.rs

+40-22
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,27 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
/// The `Drop` trait is used to run some code when a value goes out of scope.
11+
/// Used to run some code when a value goes out of scope.
1212
/// This is sometimes called a 'destructor'.
1313
///
14-
/// When a value goes out of scope, if it implements this trait, it will have
15-
/// its `drop` method called. Then any fields the value contains will also
14+
/// When a value goes out of scope, it will have its `drop` method called if
15+
/// its type implements `Drop`. Then, any fields the value contains will also
1616
/// be dropped recursively.
1717
///
18-
/// Because of the recursive dropping, you do not need to implement this trait
18+
/// Because of this recursive dropping, you do not need to implement this trait
1919
/// unless your type needs its own destructor logic.
2020
///
21+
/// Refer to [the chapter on `Drop` in *The Rust Programming Language*][book]
22+
/// for some more elaboration.
23+
///
24+
/// [book]: ../../book/second-edition/ch15-03-drop.html
25+
///
2126
/// # Examples
2227
///
23-
/// A trivial implementation of `Drop`. The `drop` method is called when `_x`
24-
/// goes out of scope, and therefore `main` prints `Dropping!`.
28+
/// ## Implementing `Drop`
29+
///
30+
/// The `drop` method is called when `_x` goes out of scope, and therefore
31+
/// `main` prints `Dropping!`.
2532
///
2633
/// ```
2734
/// struct HasDrop;
@@ -37,9 +44,11 @@
3744
/// }
3845
/// ```
3946
///
40-
/// Showing the recursive nature of `Drop`. When `outer` goes out of scope, the
41-
/// `drop` method will be called first for `Outer`, then for `Inner`. Therefore
42-
/// `main` prints `Dropping Outer!` and then `Dropping Inner!`.
47+
/// ## Dropping is done recursively
48+
///
49+
/// When `outer` goes out of scope, the `drop` method will be called first for
50+
/// `Outer`, then for `Inner`. Therefore, `main` prints `Dropping Outer!` and
51+
/// then `Dropping Inner!`.
4352
///
4453
/// ```
4554
/// struct Inner;
@@ -62,12 +71,20 @@
6271
/// }
6372
/// ```
6473
///
65-
/// Because variables are dropped in the reverse order they are declared,
66-
/// `main` will print `Declared second!` and then `Declared first!`.
74+
/// ## Variables are dropped in reverse order of declaration
75+
///
76+
/// `_first` is declared first and `_second` is declared second, so `main` will
77+
/// print `Declared second!` and then `Declared first!`.
6778
///
6879
/// ```
6980
/// struct PrintOnDrop(&'static str);
7081
///
82+
/// impl Drop for PrintOnDrop {
83+
/// fn drop(&mut self) {
84+
/// println!("{}", self.0);
85+
/// }
86+
/// }
87+
///
7188
/// fn main() {
7289
/// let _first = PrintOnDrop("Declared first!");
7390
/// let _second = PrintOnDrop("Declared second!");
@@ -76,24 +93,25 @@
7693
#[lang = "drop"]
7794
#[stable(feature = "rust1", since = "1.0.0")]
7895
pub trait Drop {
79-
/// A method called when the value goes out of scope.
96+
/// Executes the destructor for this type.
97+
///
98+
/// This method is called implilcitly when the value goes out of scope,
99+
/// and cannot be called explicitly (this is compiler error [E0040]).
100+
/// However, the [`std::mem::drop`] function in the prelude can be
101+
/// used to call the argument's `Drop` implementation.
80102
///
81103
/// When this method has been called, `self` has not yet been deallocated.
82-
/// If it were, `self` would be a dangling reference.
104+
/// That only happens after the method is over.
105+
/// If this wasn't the case, `self` would be a dangling reference.
83106
///
84-
/// After this function is over, the memory of `self` will be deallocated.
107+
/// # Panics
85108
///
86-
/// This function cannot be called explicitly. This is compiler error
87-
/// [E0040]. However, the [`std::mem::drop`] function in the prelude can be
88-
/// used to call the argument's `Drop` implementation.
109+
/// Given that a [`panic!`] will call `drop` as it unwinds, any [`panic!`]
110+
/// in a `drop` implementation will likely abort.
89111
///
90112
/// [E0040]: ../../error-index.html#E0040
113+
/// [`panic!`]: ../macro.panic.html
91114
/// [`std::mem::drop`]: ../../std/mem/fn.drop.html
92-
///
93-
/// # Panics
94-
///
95-
/// Given that a `panic!` will call `drop()` as it unwinds, any `panic!` in
96-
/// a `drop()` implementation will likely abort.
97115
#[stable(feature = "rust1", since = "1.0.0")]
98116
fn drop(&mut self);
99117
}

0 commit comments

Comments
 (0)