Skip to content

Commit 6bdba82

Browse files
author
lukaramu
committed
std::ops docs: incorporated changes suggested in review
* fixed link typos and copy-paster errors * rewrote Fn* explanations * `RHS = Self` -> `RHS` is `Self` (added that to all applicable places as well) * fixed up some links * s/MutDeref/DerefMut * removed remaining superfluous `fn main()`s * fixed some minor phrasings and factual errors and inaccuracies std::ops docs: Fix phrasing and factual errors/inaccuracies
1 parent 99e44d8 commit 6bdba82

File tree

6 files changed

+82
-58
lines changed

6 files changed

+82
-58
lines changed

src/libcore/ops/arith.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
/// The addition operator `+`.
1212
///
13-
/// Note that `RHS = Self` by default, but this is not mandatory. For example,
14-
/// [`std::time::SystemTime`] implements `Add<Duration>`, which permits
13+
/// Note that `RHS` is `Self` by default, but this is not mandatory. For
14+
/// example, [`std::time::SystemTime`] implements `Add<Duration>`, which permits
1515
/// operations of the form `SystemTime = SystemTime + Duration`.
1616
///
1717
/// [`std::time::SystemTime`]: ../../std/time/struct.SystemTime.html
@@ -105,8 +105,8 @@ add_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
105105

106106
/// The subtraction operator `-`.
107107
///
108-
/// Note that `RHS = Self` by default, but this is not mandatory. For example,
109-
/// [std::time::SystemTime] implements `Sub<Duration>`, which permits
108+
/// Note that `RHS` is `Self` by default, but this is not mandatory. For
109+
/// example, [`std::time::SystemTime`] implements `Sub<Duration>`, which permits
110110
/// operations of the form `SystemTime = SystemTime - Duration`.
111111
///
112112
/// [`std::time::SystemTime`]: ../../std/time/struct.SystemTime.html
@@ -200,7 +200,7 @@ sub_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
200200

201201
/// The multiplication operator `*`.
202202
///
203-
/// Note that `RHS = Self` by default, but this is not mandatory.
203+
/// Note that `RHS` is `Self` by default, but this is not mandatory.
204204
///
205205
/// # Examples
206206
///
@@ -317,7 +317,7 @@ mul_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
317317

318318
/// The division operator `/`.
319319
///
320-
/// Note that `RHS = Self` by default, but this is not mandatory.
320+
/// Note that `RHS` is `Self` by default, but this is not mandatory.
321321
///
322322
/// # Examples
323323
///
@@ -455,6 +455,8 @@ div_impl_float! { f32 f64 }
455455

456456
/// The remainder operator `%`.
457457
///
458+
/// Note that `RHS` is `Self` by default, but this is not mandatory.
459+
///
458460
/// # Examples
459461
///
460462
/// This example implements `Rem` on a `SplitSlice` object. After `Rem` is

src/libcore/ops/bit.rs

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ not_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
6868

6969
/// The bitwise AND operator `&`.
7070
///
71+
/// Note that `RHS` is `Self` by default, but this is not mandatory.
72+
///
7173
/// # Examples
7274
///
7375
/// An implementation of `BitAnd` for a wrapper around `bool`.
@@ -147,6 +149,8 @@ bitand_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
147149

148150
/// The bitwise OR operator `|`.
149151
///
152+
/// Note that `RHS` is `Self` by default, but this is not mandatory.
153+
///
150154
/// # Examples
151155
///
152156
/// An implementation of `BitOr` for a wrapper around `bool`.
@@ -226,6 +230,8 @@ bitor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
226230

227231
/// The bitwise XOR operator `^`.
228232
///
233+
/// Note that `RHS` is `Self` by default, but this is not mandatory.
234+
///
229235
/// # Examples
230236
///
231237
/// An implementation of `BitXor` that lifts `^` to a wrapper around `bool`.

src/libcore/ops/deref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'a, T: ?Sized> Deref for &'a mut T {
111111
///
112112
/// # More on `Deref` coercion
113113
///
114-
/// If `T` implements `MutDeref<Target = U>`, and `x` is a value of type `T`,
114+
/// If `T` implements `DerefMut<Target = U>`, and `x` is a value of type `T`,
115115
/// then:
116116
/// * In mutable contexts, `*x` on non-pointer types is equivalent to
117117
/// `*Deref::deref(&x)`.

src/libcore/ops/function.rs

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

11-
/// A version of the call operator that takes an immutable receiver.
11+
/// The version of the call operator that takes an immutable receiver.
1212
///
13-
/// Closures only taking immutable references to captured variables
14-
/// automatically implement this trait, which allows them to be invoked.
15-
/// For mutably referenced captures, see [`FnMut`], and for consuming the
16-
/// capture, see [`FnOnce`].
13+
/// Instances of `Fn` can be called repeatedly without mutating state.
1714
///
18-
/// You can use the [`Fn`] traits when you want to accept a closure as a
19-
/// parameter. Since both [`FnMut`] and [`FnOnce`] are supertraits of `Fn`, any
20-
/// instance of `Fn` can be used where a [`FnMut`] or [`FnOnce`] is expected.
15+
/// *This trait (`Fn`) is not to be confused with [function pointers][]
16+
/// (`fn`).*
17+
///
18+
/// `Fn` is implemented automatically by closures which only take immutable
19+
/// references to captured variables or don't capture anything at all, as well
20+
/// as (safe) [function pointers][] (with some caveats, see their documentation
21+
/// for more details). Additionally, for any type `F` that implements `Fn`, `&F`
22+
/// implements `Fn`, too.
23+
///
24+
/// Since both [`FnMut`] and [`FnOnce`] are supertraits of `Fn`, any
25+
/// instance of `Fn` can be used as a parameter where a [`FnMut`] or [`FnOnce`]
26+
/// is expected.
27+
///
28+
/// Use `Fn` as a bound when you want to accept a parameter of function-like
29+
/// type and need to call it repeatedly and without mutating state (e.g. when
30+
/// calling it concurrently). If you do not need such strict requirements, use
31+
/// [`FnMut`] or [`FnOnce`] as bounds.
2132
///
2233
/// See the [chapter on closures in *The Rust Programming Language*][book] for
23-
/// more information about closures in general.
34+
/// some more information on this topic.
2435
///
2536
/// Also of note is the special syntax for `Fn` traits (e.g.
2637
/// `Fn(usize, bool) -> usize`). Those interested in the technical details of
27-
/// this can refer to [the relevant section in *The Rustonomicon*][nomicon].
38+
/// this can refer to [the relevant section in the *Rustonomicon*][nomicon].
2839
///
2940
/// [book]: ../../book/second-edition/ch13-01-closures.html
3041
/// [`FnMut`]: trait.FnMut.html
3142
/// [`FnOnce`]: trait.FnOnce.html
43+
/// [function pointers]: ../../std/primitive.fn.html
3244
/// [nomicon]: ../../nomicon/hrtb.html
3345
///
3446
/// # Examples
@@ -61,29 +73,36 @@ pub trait Fn<Args> : FnMut<Args> {
6173
extern "rust-call" fn call(&self, args: Args) -> Self::Output;
6274
}
6375

64-
/// A version of the call operator that takes a mutable receiver.
76+
/// The version of the call operator that takes a mutable receiver.
77+
///
78+
/// Instances of `FnMut` can be called repeatedly and may mutate state.
6579
///
66-
/// Closures that might mutably reference captured variables automatically
67-
/// implement this trait, which allows them to be invoked. For immutably
68-
/// referenced captures, see [`Fn`], and for consuming the captures, see
69-
/// [`FnOnce`].
80+
/// `FnMut` is implemented automatically by closures which take mutable
81+
/// references to captured variables, as well as all types that implement
82+
/// [`Fn`], e.g. (safe) [function pointers][] (since `FnMut` is a supertrait of
83+
/// [`Fn`]). Additionally, for any type `F` that implements `FnMut`, `&mut F`
84+
/// implements `FnMut`, too.
7085
///
71-
/// You can use the [`Fn`] traits when you want to accept a closure as a
72-
/// parameter. Since [`FnOnce`] is a supertrait of `FnMut`, any instance of
73-
/// `FnMut` can be used where a [`FnOnce`] is expected, and since [`Fn`] is a
74-
/// subtrait of `FnMut`, any instance of [`Fn`] can be used where [`FnMut`] is
75-
/// expected.
86+
/// Since [`FnOnce`] is a supertrait of `FnMut`, any instance of `FnMut` can be
87+
/// used where a [`FnOnce`] is expected, and since [`Fn`] is a subtrait of
88+
/// `FnMut`, any instance of [`Fn`] can be used where `FnMut` is expected.
89+
///
90+
/// Use `FnMut` as a bound when you want to accept a parameter of function-like
91+
/// type and need to call it repeatedly, while allowing it to mutate state.
92+
/// If you don't want the parameter to mutate state, use [`Fn`] as a
93+
/// bound; if you don't need to call it repeatedly, use [`FnOnce`].
7694
///
7795
/// See the [chapter on closures in *The Rust Programming Language*][book] for
78-
/// more information about closures in general.
96+
/// some more information on this topic.
7997
///
8098
/// Also of note is the special syntax for `Fn` traits (e.g.
8199
/// `Fn(usize, bool) -> usize`). Those interested in the technical details of
82-
/// this can refer to [the relevant section in *The Rustonomicon*][nomicon].
100+
/// this can refer to [the relevant section in the *Rustonomicon*][nomicon].
83101
///
84102
/// [book]: ../../book/second-edition/ch13-01-closures.html
85-
/// [`Fn`]: trait.Fnhtml
103+
/// [`Fn`]: trait.Fn.html
86104
/// [`FnOnce`]: trait.FnOnce.html
105+
/// [function pointers]: ../../std/primitive.fn.html
87106
/// [nomicon]: ../../nomicon/hrtb.html
88107
///
89108
/// # Examples
@@ -127,27 +146,35 @@ pub trait FnMut<Args> : FnOnce<Args> {
127146
extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
128147
}
129148

130-
/// A version of the call operator that takes a by-value receiver.
149+
/// The version of the call operator that takes a by-value receiver.
150+
///
151+
/// Instances of `FnOnce` can be called, but might not be callable multiple
152+
/// times. Because of this, if the only thing known about a type is that it
153+
/// implements `FnOnce`, it can only be called once.
154+
///
155+
/// `FnOnce` is implemented automatically by closure that might consume captured
156+
/// variables, as well as all types that implement [`FnMut`], e.g. (safe)
157+
/// [function pointers][] (since `FnOnce` is a supertrait of [`FnMut`]).
131158
///
132-
/// Closures that might take ownership of captured variables automatically
133-
/// implement this trait, which allows them to be invoked. For immutably
134-
/// referenced captures, see [`Fn`], and for mutably referenced captures,
135-
/// see [`FnMut`].
159+
/// Since both [`Fn`] and [`FnMut`] are subtraits of `FnOnce`, any instance of
160+
/// [`Fn`] or [`FnMut`] can be used where a `FnOnce` is expected.
136161
///
137-
/// You can use the [`Fn`] traits when you want to accept a closure as a
138-
/// parameter. Since both [`Fn`] and [`FnMut`] are subtraits of `FnOnce`, any
139-
/// instance of [`Fn`] or [`FnMut`] can be used where a `FnOnce` is expected.
162+
/// Use `FnOnce` as a bound when you want to accept a parameter of function-like
163+
/// type and only need to call it once. If you need to call the parameter
164+
/// repeatedly, use [`FnMut`] as a bound; if you also need it to not mutate
165+
/// state, use [`Fn`].
140166
///
141167
/// See the [chapter on closures in *The Rust Programming Language*][book] for
142-
/// more information about closures in general.
168+
/// some more information on this topic.
143169
///
144170
/// Also of note is the special syntax for `Fn` traits (e.g.
145171
/// `Fn(usize, bool) -> usize`). Those interested in the technical details of
146-
/// this can refer to [the relevant section in *The Rustonomicon*][nomicon].
172+
/// this can refer to [the relevant section in the *Rustonomicon*][nomicon].
147173
///
148174
/// [book]: ../../book/second-edition/ch13-01-closures.html
149175
/// [`Fn`]: trait.Fn.html
150176
/// [`FnMut`]: trait.FnMut.html
177+
/// [function pointers]: ../../std/primitive.fn.html
151178
/// [nomicon]: ../../nomicon/hrtb.html
152179
///
153180
/// # Examples

src/libcore/ops/index.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ pub trait Index<Idx: ?Sized> {
7777
/// `container[index]` is actually syntactic sugar for
7878
/// `*container.index_mut(index)`, but only when used as a mutable value. If
7979
/// an immutable value is requested, the [`Index`] trait is used instead. This
80-
/// allows nice things such as `v[index] = value` if the type of `value`
81-
/// implements [`Copy`].
80+
/// allows nice things such as `v[index] = value`.
8281
///
8382
/// [`Index`]: ../../std/ops/trait.Index.html
84-
/// [`Copy`]: ../../std/marker/trait.Copy.html
8583
///
8684
/// # Examples
8785
///

src/libcore/ops/range.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use fmt;
1212

1313
/// An unbounded range (`..`).
1414
///
15-
/// `RangeFull` is primarily used as a [slicing index], it's shorthand is `..`.
15+
/// `RangeFull` is primarily used as a [slicing index], its shorthand is `..`.
1616
/// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
1717
///
1818
/// # Examples
@@ -101,15 +101,13 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
101101
/// ```
102102
/// #![feature(range_contains)]
103103
///
104-
/// # fn main() {
105104
/// assert!(!(3..5).contains(2));
106105
/// assert!( (3..5).contains(3));
107106
/// assert!( (3..5).contains(4));
108107
/// assert!(!(3..5).contains(5));
109108
///
110109
/// assert!(!(3..3).contains(3));
111110
/// assert!(!(3..2).contains(3));
112-
/// # }
113111
/// ```
114112
pub fn contains(&self, item: Idx) -> bool {
115113
(self.start <= item) && (item < self.end)
@@ -163,11 +161,9 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
163161
/// ```
164162
/// #![feature(range_contains)]
165163
///
166-
/// # fn main() {
167164
/// assert!(!(3..).contains(2));
168165
/// assert!( (3..).contains(3));
169166
/// assert!( (3..).contains(1_000_000_000));
170-
/// # }
171167
/// ```
172168
pub fn contains(&self, item: Idx) -> bool {
173169
(self.start <= item)
@@ -191,6 +187,8 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
191187
/// a `for` loop directly. This won't compile:
192188
///
193189
/// ```compile_fail,E0277
190+
/// // error[E0277]: the trait bound `std::ops::RangeTo<{integer}>:
191+
/// // std::iter::Iterator` is not satisfied
194192
/// for i in ..5 {
195193
/// // ...
196194
/// }
@@ -234,11 +232,9 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
234232
/// ```
235233
/// #![feature(range_contains)]
236234
///
237-
/// # fn main() {
238235
/// assert!( (..5).contains(-1_000_000_000));
239236
/// assert!( (..5).contains(4));
240237
/// assert!(!(..5).contains(5));
241-
/// # }
242238
/// ```
243239
pub fn contains(&self, item: Idx) -> bool {
244240
(item < self.end)
@@ -255,14 +251,12 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
255251
/// ```
256252
/// #![feature(inclusive_range,inclusive_range_syntax)]
257253
///
258-
/// # fn main() {
259254
/// assert_eq!((3...5), std::ops::RangeInclusive { start: 3, end: 5 });
260255
/// assert_eq!(3 + 4 + 5, (3...5).sum());
261256
///
262257
/// let arr = [0, 1, 2, 3];
263258
/// assert_eq!(arr[ ...2], [0,1,2 ]);
264259
/// assert_eq!(arr[1...2], [ 1,2 ]); // RangeInclusive
265-
/// # }
266260
/// ```
267261
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
268262
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
@@ -295,7 +289,6 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
295289
/// ```
296290
/// #![feature(range_contains,inclusive_range_syntax)]
297291
///
298-
/// # fn main() {
299292
/// assert!(!(3...5).contains(2));
300293
/// assert!( (3...5).contains(3));
301294
/// assert!( (3...5).contains(4));
@@ -304,7 +297,6 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
304297
///
305298
/// assert!( (3...3).contains(3));
306299
/// assert!(!(3...2).contains(3));
307-
/// # }
308300
/// ```
309301
pub fn contains(&self, item: Idx) -> bool {
310302
self.start <= item && item <= self.end
@@ -330,6 +322,9 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
330322
///
331323
/// ```compile_fail,E0277
332324
/// #![feature(inclusive_range_syntax)]
325+
///
326+
/// // error[E0277]: the trait bound `std::ops::RangeToInclusive<{integer}>:
327+
/// // std::iter::Iterator` is not satisfied
333328
/// for i in ...5 {
334329
/// // ...
335330
/// }
@@ -341,11 +336,9 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
341336
/// ```
342337
/// #![feature(inclusive_range_syntax)]
343338
///
344-
/// # fn main() {
345339
/// let arr = [0, 1, 2, 3];
346340
/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
347341
/// assert_eq!(arr[1...2], [ 1,2 ]);
348-
/// # }
349342
/// ```
350343
///
351344
/// [`IntoIterator`]: ../iter/trait.Iterator.html
@@ -377,11 +370,9 @@ impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
377370
/// ```
378371
/// #![feature(range_contains,inclusive_range_syntax)]
379372
///
380-
/// # fn main() {
381373
/// assert!( (...5).contains(-1_000_000_000));
382374
/// assert!( (...5).contains(5));
383375
/// assert!(!(...5).contains(6));
384-
/// # }
385376
/// ```
386377
pub fn contains(&self, item: Idx) -> bool {
387378
(item <= self.end)

0 commit comments

Comments
 (0)