1
Fork 0

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
This commit is contained in:
lukaramu 2017-08-08 14:34:37 +02:00
parent 99e44d8680
commit 6bdba82ba1
6 changed files with 82 additions and 58 deletions

View file

@ -10,8 +10,8 @@
/// The addition operator `+`. /// The addition operator `+`.
/// ///
/// Note that `RHS = Self` by default, but this is not mandatory. For example, /// Note that `RHS` is `Self` by default, but this is not mandatory. For
/// [`std::time::SystemTime`] implements `Add<Duration>`, which permits /// example, [`std::time::SystemTime`] implements `Add<Duration>`, which permits
/// operations of the form `SystemTime = SystemTime + Duration`. /// operations of the form `SystemTime = SystemTime + Duration`.
/// ///
/// [`std::time::SystemTime`]: ../../std/time/struct.SystemTime.html /// [`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 }
/// The subtraction operator `-`. /// The subtraction operator `-`.
/// ///
/// Note that `RHS = Self` by default, but this is not mandatory. For example, /// Note that `RHS` is `Self` by default, but this is not mandatory. For
/// [std::time::SystemTime] implements `Sub<Duration>`, which permits /// example, [`std::time::SystemTime`] implements `Sub<Duration>`, which permits
/// operations of the form `SystemTime = SystemTime - Duration`. /// operations of the form `SystemTime = SystemTime - Duration`.
/// ///
/// [`std::time::SystemTime`]: ../../std/time/struct.SystemTime.html /// [`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 }
/// The multiplication operator `*`. /// The multiplication operator `*`.
/// ///
/// Note that `RHS = Self` by default, but this is not mandatory. /// Note that `RHS` is `Self` by default, but this is not mandatory.
/// ///
/// # Examples /// # Examples
/// ///
@ -317,7 +317,7 @@ mul_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
/// The division operator `/`. /// The division operator `/`.
/// ///
/// Note that `RHS = Self` by default, but this is not mandatory. /// Note that `RHS` is `Self` by default, but this is not mandatory.
/// ///
/// # Examples /// # Examples
/// ///
@ -455,6 +455,8 @@ div_impl_float! { f32 f64 }
/// The remainder operator `%`. /// The remainder operator `%`.
/// ///
/// Note that `RHS` is `Self` by default, but this is not mandatory.
///
/// # Examples /// # Examples
/// ///
/// This example implements `Rem` on a `SplitSlice` object. After `Rem` is /// This example implements `Rem` on a `SplitSlice` object. After `Rem` is

View file

@ -68,6 +68,8 @@ not_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
/// The bitwise AND operator `&`. /// The bitwise AND operator `&`.
/// ///
/// Note that `RHS` is `Self` by default, but this is not mandatory.
///
/// # Examples /// # Examples
/// ///
/// An implementation of `BitAnd` for a wrapper around `bool`. /// 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 }
/// The bitwise OR operator `|`. /// The bitwise OR operator `|`.
/// ///
/// Note that `RHS` is `Self` by default, but this is not mandatory.
///
/// # Examples /// # Examples
/// ///
/// An implementation of `BitOr` for a wrapper around `bool`. /// 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 }
/// The bitwise XOR operator `^`. /// The bitwise XOR operator `^`.
/// ///
/// Note that `RHS` is `Self` by default, but this is not mandatory.
///
/// # Examples /// # Examples
/// ///
/// An implementation of `BitXor` that lifts `^` to a wrapper around `bool`. /// An implementation of `BitXor` that lifts `^` to a wrapper around `bool`.

View file

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

View file

@ -8,27 +8,39 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
/// A version of the call operator that takes an immutable receiver. /// The version of the call operator that takes an immutable receiver.
/// ///
/// Closures only taking immutable references to captured variables /// Instances of `Fn` can be called repeatedly without mutating state.
/// automatically implement this trait, which allows them to be invoked.
/// For mutably referenced captures, see [`FnMut`], and for consuming the
/// capture, see [`FnOnce`].
/// ///
/// You can use the [`Fn`] traits when you want to accept a closure as a /// *This trait (`Fn`) is not to be confused with [function pointers][]
/// parameter. Since both [`FnMut`] and [`FnOnce`] are supertraits of `Fn`, any /// (`fn`).*
/// instance of `Fn` can be used where a [`FnMut`] or [`FnOnce`] is expected. ///
/// `Fn` is implemented automatically by closures which only take immutable
/// references to captured variables or don't capture anything at all, as well
/// as (safe) [function pointers][] (with some caveats, see their documentation
/// for more details). Additionally, for any type `F` that implements `Fn`, `&F`
/// implements `Fn`, too.
///
/// Since both [`FnMut`] and [`FnOnce`] are supertraits of `Fn`, any
/// instance of `Fn` can be used as a parameter where a [`FnMut`] or [`FnOnce`]
/// is expected.
///
/// Use `Fn` as a bound when you want to accept a parameter of function-like
/// type and need to call it repeatedly and without mutating state (e.g. when
/// calling it concurrently). If you do not need such strict requirements, use
/// [`FnMut`] or [`FnOnce`] as bounds.
/// ///
/// See the [chapter on closures in *The Rust Programming Language*][book] for /// See the [chapter on closures in *The Rust Programming Language*][book] for
/// more information about closures in general. /// some more information on this topic.
/// ///
/// Also of note is the special syntax for `Fn` traits (e.g. /// Also of note is the special syntax for `Fn` traits (e.g.
/// `Fn(usize, bool) -> usize`). Those interested in the technical details of /// `Fn(usize, bool) -> usize`). Those interested in the technical details of
/// this can refer to [the relevant section in *The Rustonomicon*][nomicon]. /// this can refer to [the relevant section in the *Rustonomicon*][nomicon].
/// ///
/// [book]: ../../book/second-edition/ch13-01-closures.html /// [book]: ../../book/second-edition/ch13-01-closures.html
/// [`FnMut`]: trait.FnMut.html /// [`FnMut`]: trait.FnMut.html
/// [`FnOnce`]: trait.FnOnce.html /// [`FnOnce`]: trait.FnOnce.html
/// [function pointers]: ../../std/primitive.fn.html
/// [nomicon]: ../../nomicon/hrtb.html /// [nomicon]: ../../nomicon/hrtb.html
/// ///
/// # Examples /// # Examples
@ -61,29 +73,36 @@ pub trait Fn<Args> : FnMut<Args> {
extern "rust-call" fn call(&self, args: Args) -> Self::Output; extern "rust-call" fn call(&self, args: Args) -> Self::Output;
} }
/// A version of the call operator that takes a mutable receiver. /// The version of the call operator that takes a mutable receiver.
/// ///
/// Closures that might mutably reference captured variables automatically /// Instances of `FnMut` can be called repeatedly and may mutate state.
/// implement this trait, which allows them to be invoked. For immutably
/// referenced captures, see [`Fn`], and for consuming the captures, see
/// [`FnOnce`].
/// ///
/// You can use the [`Fn`] traits when you want to accept a closure as a /// `FnMut` is implemented automatically by closures which take mutable
/// parameter. Since [`FnOnce`] is a supertrait of `FnMut`, any instance of /// references to captured variables, as well as all types that implement
/// `FnMut` can be used where a [`FnOnce`] is expected, and since [`Fn`] is a /// [`Fn`], e.g. (safe) [function pointers][] (since `FnMut` is a supertrait of
/// subtrait of `FnMut`, any instance of [`Fn`] can be used where [`FnMut`] is /// [`Fn`]). Additionally, for any type `F` that implements `FnMut`, `&mut F`
/// expected. /// implements `FnMut`, too.
///
/// Since [`FnOnce`] is a supertrait of `FnMut`, any instance of `FnMut` can be
/// used where a [`FnOnce`] is expected, and since [`Fn`] is a subtrait of
/// `FnMut`, any instance of [`Fn`] can be used where `FnMut` is expected.
///
/// Use `FnMut` as a bound when you want to accept a parameter of function-like
/// type and need to call it repeatedly, while allowing it to mutate state.
/// If you don't want the parameter to mutate state, use [`Fn`] as a
/// bound; if you don't need to call it repeatedly, use [`FnOnce`].
/// ///
/// See the [chapter on closures in *The Rust Programming Language*][book] for /// See the [chapter on closures in *The Rust Programming Language*][book] for
/// more information about closures in general. /// some more information on this topic.
/// ///
/// Also of note is the special syntax for `Fn` traits (e.g. /// Also of note is the special syntax for `Fn` traits (e.g.
/// `Fn(usize, bool) -> usize`). Those interested in the technical details of /// `Fn(usize, bool) -> usize`). Those interested in the technical details of
/// this can refer to [the relevant section in *The Rustonomicon*][nomicon]. /// this can refer to [the relevant section in the *Rustonomicon*][nomicon].
/// ///
/// [book]: ../../book/second-edition/ch13-01-closures.html /// [book]: ../../book/second-edition/ch13-01-closures.html
/// [`Fn`]: trait.Fnhtml /// [`Fn`]: trait.Fn.html
/// [`FnOnce`]: trait.FnOnce.html /// [`FnOnce`]: trait.FnOnce.html
/// [function pointers]: ../../std/primitive.fn.html
/// [nomicon]: ../../nomicon/hrtb.html /// [nomicon]: ../../nomicon/hrtb.html
/// ///
/// # Examples /// # Examples
@ -127,27 +146,35 @@ pub trait FnMut<Args> : FnOnce<Args> {
extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output; extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
} }
/// A version of the call operator that takes a by-value receiver. /// The version of the call operator that takes a by-value receiver.
/// ///
/// Closures that might take ownership of captured variables automatically /// Instances of `FnOnce` can be called, but might not be callable multiple
/// implement this trait, which allows them to be invoked. For immutably /// times. Because of this, if the only thing known about a type is that it
/// referenced captures, see [`Fn`], and for mutably referenced captures, /// implements `FnOnce`, it can only be called once.
/// see [`FnMut`].
/// ///
/// You can use the [`Fn`] traits when you want to accept a closure as a /// `FnOnce` is implemented automatically by closure that might consume captured
/// parameter. Since both [`Fn`] and [`FnMut`] are subtraits of `FnOnce`, any /// variables, as well as all types that implement [`FnMut`], e.g. (safe)
/// instance of [`Fn`] or [`FnMut`] can be used where a `FnOnce` is expected. /// [function pointers][] (since `FnOnce` is a supertrait of [`FnMut`]).
///
/// Since both [`Fn`] and [`FnMut`] are subtraits of `FnOnce`, any instance of
/// [`Fn`] or [`FnMut`] can be used where a `FnOnce` is expected.
///
/// Use `FnOnce` as a bound when you want to accept a parameter of function-like
/// type and only need to call it once. If you need to call the parameter
/// repeatedly, use [`FnMut`] as a bound; if you also need it to not mutate
/// state, use [`Fn`].
/// ///
/// See the [chapter on closures in *The Rust Programming Language*][book] for /// See the [chapter on closures in *The Rust Programming Language*][book] for
/// more information about closures in general. /// some more information on this topic.
/// ///
/// Also of note is the special syntax for `Fn` traits (e.g. /// Also of note is the special syntax for `Fn` traits (e.g.
/// `Fn(usize, bool) -> usize`). Those interested in the technical details of /// `Fn(usize, bool) -> usize`). Those interested in the technical details of
/// this can refer to [the relevant section in *The Rustonomicon*][nomicon]. /// this can refer to [the relevant section in the *Rustonomicon*][nomicon].
/// ///
/// [book]: ../../book/second-edition/ch13-01-closures.html /// [book]: ../../book/second-edition/ch13-01-closures.html
/// [`Fn`]: trait.Fn.html /// [`Fn`]: trait.Fn.html
/// [`FnMut`]: trait.FnMut.html /// [`FnMut`]: trait.FnMut.html
/// [function pointers]: ../../std/primitive.fn.html
/// [nomicon]: ../../nomicon/hrtb.html /// [nomicon]: ../../nomicon/hrtb.html
/// ///
/// # Examples /// # Examples

View file

@ -77,11 +77,9 @@ pub trait Index<Idx: ?Sized> {
/// `container[index]` is actually syntactic sugar for /// `container[index]` is actually syntactic sugar for
/// `*container.index_mut(index)`, but only when used as a mutable value. If /// `*container.index_mut(index)`, but only when used as a mutable value. If
/// an immutable value is requested, the [`Index`] trait is used instead. This /// an immutable value is requested, the [`Index`] trait is used instead. This
/// allows nice things such as `v[index] = value` if the type of `value` /// allows nice things such as `v[index] = value`.
/// implements [`Copy`].
/// ///
/// [`Index`]: ../../std/ops/trait.Index.html /// [`Index`]: ../../std/ops/trait.Index.html
/// [`Copy`]: ../../std/marker/trait.Copy.html
/// ///
/// # Examples /// # Examples
/// ///

View file

@ -12,7 +12,7 @@ use fmt;
/// An unbounded range (`..`). /// An unbounded range (`..`).
/// ///
/// `RangeFull` is primarily used as a [slicing index], it's shorthand is `..`. /// `RangeFull` is primarily used as a [slicing index], its shorthand is `..`.
/// It cannot serve as an [`Iterator`] because it doesn't have a starting point. /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
/// ///
/// # Examples /// # Examples
@ -101,7 +101,6 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
/// ``` /// ```
/// #![feature(range_contains)] /// #![feature(range_contains)]
/// ///
/// # fn main() {
/// assert!(!(3..5).contains(2)); /// assert!(!(3..5).contains(2));
/// assert!( (3..5).contains(3)); /// assert!( (3..5).contains(3));
/// assert!( (3..5).contains(4)); /// assert!( (3..5).contains(4));
@ -109,7 +108,6 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
/// ///
/// assert!(!(3..3).contains(3)); /// assert!(!(3..3).contains(3));
/// assert!(!(3..2).contains(3)); /// assert!(!(3..2).contains(3));
/// # }
/// ``` /// ```
pub fn contains(&self, item: Idx) -> bool { pub fn contains(&self, item: Idx) -> bool {
(self.start <= item) && (item < self.end) (self.start <= item) && (item < self.end)
@ -163,11 +161,9 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
/// ``` /// ```
/// #![feature(range_contains)] /// #![feature(range_contains)]
/// ///
/// # fn main() {
/// assert!(!(3..).contains(2)); /// assert!(!(3..).contains(2));
/// assert!( (3..).contains(3)); /// assert!( (3..).contains(3));
/// assert!( (3..).contains(1_000_000_000)); /// assert!( (3..).contains(1_000_000_000));
/// # }
/// ``` /// ```
pub fn contains(&self, item: Idx) -> bool { pub fn contains(&self, item: Idx) -> bool {
(self.start <= item) (self.start <= item)
@ -191,6 +187,8 @@ impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
/// a `for` loop directly. This won't compile: /// a `for` loop directly. This won't compile:
/// ///
/// ```compile_fail,E0277 /// ```compile_fail,E0277
/// // error[E0277]: the trait bound `std::ops::RangeTo<{integer}>:
/// // std::iter::Iterator` is not satisfied
/// for i in ..5 { /// for i in ..5 {
/// // ... /// // ...
/// } /// }
@ -234,11 +232,9 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
/// ``` /// ```
/// #![feature(range_contains)] /// #![feature(range_contains)]
/// ///
/// # fn main() {
/// assert!( (..5).contains(-1_000_000_000)); /// assert!( (..5).contains(-1_000_000_000));
/// assert!( (..5).contains(4)); /// assert!( (..5).contains(4));
/// assert!(!(..5).contains(5)); /// assert!(!(..5).contains(5));
/// # }
/// ``` /// ```
pub fn contains(&self, item: Idx) -> bool { pub fn contains(&self, item: Idx) -> bool {
(item < self.end) (item < self.end)
@ -255,14 +251,12 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
/// ``` /// ```
/// #![feature(inclusive_range,inclusive_range_syntax)] /// #![feature(inclusive_range,inclusive_range_syntax)]
/// ///
/// # fn main() {
/// assert_eq!((3...5), std::ops::RangeInclusive { start: 3, end: 5 }); /// assert_eq!((3...5), std::ops::RangeInclusive { start: 3, end: 5 });
/// assert_eq!(3 + 4 + 5, (3...5).sum()); /// assert_eq!(3 + 4 + 5, (3...5).sum());
/// ///
/// let arr = [0, 1, 2, 3]; /// let arr = [0, 1, 2, 3];
/// assert_eq!(arr[ ...2], [0,1,2 ]); /// assert_eq!(arr[ ...2], [0,1,2 ]);
/// assert_eq!(arr[1...2], [ 1,2 ]); // RangeInclusive /// assert_eq!(arr[1...2], [ 1,2 ]); // RangeInclusive
/// # }
/// ``` /// ```
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186 #[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
@ -295,7 +289,6 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// ``` /// ```
/// #![feature(range_contains,inclusive_range_syntax)] /// #![feature(range_contains,inclusive_range_syntax)]
/// ///
/// # fn main() {
/// assert!(!(3...5).contains(2)); /// assert!(!(3...5).contains(2));
/// assert!( (3...5).contains(3)); /// assert!( (3...5).contains(3));
/// assert!( (3...5).contains(4)); /// assert!( (3...5).contains(4));
@ -304,7 +297,6 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// ///
/// assert!( (3...3).contains(3)); /// assert!( (3...3).contains(3));
/// assert!(!(3...2).contains(3)); /// assert!(!(3...2).contains(3));
/// # }
/// ``` /// ```
pub fn contains(&self, item: Idx) -> bool { pub fn contains(&self, item: Idx) -> bool {
self.start <= item && item <= self.end self.start <= item && item <= self.end
@ -330,6 +322,9 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// ///
/// ```compile_fail,E0277 /// ```compile_fail,E0277
/// #![feature(inclusive_range_syntax)] /// #![feature(inclusive_range_syntax)]
///
/// // error[E0277]: the trait bound `std::ops::RangeToInclusive<{integer}>:
/// // std::iter::Iterator` is not satisfied
/// for i in ...5 { /// for i in ...5 {
/// // ... /// // ...
/// } /// }
@ -341,11 +336,9 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
/// ``` /// ```
/// #![feature(inclusive_range_syntax)] /// #![feature(inclusive_range_syntax)]
/// ///
/// # fn main() {
/// let arr = [0, 1, 2, 3]; /// let arr = [0, 1, 2, 3];
/// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive /// assert_eq!(arr[ ...2], [0,1,2 ]); // RangeToInclusive
/// assert_eq!(arr[1...2], [ 1,2 ]); /// assert_eq!(arr[1...2], [ 1,2 ]);
/// # }
/// ``` /// ```
/// ///
/// [`IntoIterator`]: ../iter/trait.Iterator.html /// [`IntoIterator`]: ../iter/trait.Iterator.html
@ -377,11 +370,9 @@ impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
/// ``` /// ```
/// #![feature(range_contains,inclusive_range_syntax)] /// #![feature(range_contains,inclusive_range_syntax)]
/// ///
/// # fn main() {
/// assert!( (...5).contains(-1_000_000_000)); /// assert!( (...5).contains(-1_000_000_000));
/// assert!( (...5).contains(5)); /// assert!( (...5).contains(5));
/// assert!(!(...5).contains(6)); /// assert!(!(...5).contains(6));
/// # }
/// ``` /// ```
pub fn contains(&self, item: Idx) -> bool { pub fn contains(&self, item: Idx) -> bool {
(item <= self.end) (item <= self.end)