Add a compile fail example, binding -> variable, apply suggestions

This commit is contained in:
Alexis Bourget 2020-06-25 10:05:30 +02:00
parent 3c46e36d39
commit 3d09017477

View File

@ -965,20 +965,21 @@ mod move_keyword {}
#[doc(keyword = "mut")] #[doc(keyword = "mut")]
// //
/// A mutable binding, reference, or pointer. /// A mutable variable, reference, or pointer.
/// ///
/// `mut` can be used in several situations. The first is mutable bindings, /// `mut` can be used in several situations. The first is mutable variables,
/// which can be used anywhere you can bind a value to a variable name. Some /// which can be used anywhere you can bind a value to a variable name. Some
/// examples: /// examples:
/// ///
/// ``` /// ```rust
/// // A mutable binding in the parameter list of a function. /// // A mutable variable in the parameter list of a function.
/// fn foo(mut x: u8, y: u8) -> u8 { /// fn foo(mut x: u8, y: u8) -> u8 {
/// x += y; /// x += y;
/// x /// x
/// } /// }
/// ///
/// // A mutable binding for a variable. /// // Modifying a mutable variable.
/// # #[allow(unused_assignments)]
/// let mut a = 5; /// let mut a = 5;
/// a = 6; /// a = 6;
/// ///
@ -986,17 +987,17 @@ mod move_keyword {}
/// assert_eq!(a, 6); /// assert_eq!(a, 6);
/// ``` /// ```
/// ///
/// The second is references. They can be created from `mut` bindings and must /// The second is mutable references. They can be created from `mut` variables
/// be unique: no other binding can have a mutable reference, nor a simple /// and must be unique: no other variables can have a mutable reference, nor a
/// reference. /// shared reference.
/// ///
/// ``` /// ```rust
/// // Taking a mutable reference. /// // Taking a mutable reference.
/// fn push_two(v: &mut Vec<u8>) { /// fn push_two(v: &mut Vec<u8>) {
/// v.push(2); /// v.push(2);
/// } /// }
/// ///
/// // You cannot take a mutable reference to a non-mutable variable. /// // A mutable reference cannot be taken to a non-mutable variable.
/// let mut v = vec![0, 1]; /// let mut v = vec![0, 1];
/// // Passing a mutable reference. /// // Passing a mutable reference.
/// push_two(&mut v); /// push_two(&mut v);
@ -1004,10 +1005,18 @@ mod move_keyword {}
/// assert_eq!(v, vec![0, 1, 2]); /// assert_eq!(v, vec![0, 1, 2]);
/// ``` /// ```
/// ///
/// Mutable pointers work much like mutable references, with the added /// ```rust,compile_fail,E0502
/// possibility of being nul. The syntax is `*mut Type`. /// let mut v = vec![0, 1];
/// let mut_ref_v = &mut v;
/// ##[allow(unused)]
/// let ref_v = &v;
/// mut_ref_v.push(2);
/// ```
/// ///
/// You can find more information on mutable references and pointers in the /// Mutable raw pointers work much like mutable references, with the added
/// possibility of not pointing to a valid object. The syntax is `*mut Type`.
///
/// More information on mutable references and pointers can be found in```
/// [Reference]. /// [Reference].
/// ///
/// [Reference]: ../reference/types/pointer.html#mutable-references-mut /// [Reference]: ../reference/types/pointer.html#mutable-references-mut