Skip to content

Commit

Permalink
Rollup merge of rust-lang#73621 - poliorcetics:mut-keyword, r=stevekl…
Browse files Browse the repository at this point in the history
…abnik

Document the mut keyword

Partial fix for rust-lang#34601.

Documentation for the `mut` keyword. I think it's okay for it to be quite short, this is not the book not the reference, but if you find something is missing, do not hesitate to tell me.
  • Loading branch information
Dylan-DPC authored Jun 25, 2020
2 parents 4c558f5 + 3d09017 commit 9a5c4d7
Showing 1 changed file with 53 additions and 3 deletions.
56 changes: 53 additions & 3 deletions src/libstd/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,11 +965,61 @@ mod move_keyword {}

#[doc(keyword = "mut")]
//
/// A mutable binding, reference, or pointer.
/// A mutable variable, reference, or pointer.
///
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
/// `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
/// examples:
///
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
/// ```rust
/// // A mutable variable in the parameter list of a function.
/// fn foo(mut x: u8, y: u8) -> u8 {
/// x += y;
/// x
/// }
///
/// // Modifying a mutable variable.
/// # #[allow(unused_assignments)]
/// let mut a = 5;
/// a = 6;
///
/// assert_eq!(foo(3, 4), 7);
/// assert_eq!(a, 6);
/// ```
///
/// The second is mutable references. They can be created from `mut` variables
/// and must be unique: no other variables can have a mutable reference, nor a
/// shared reference.
///
/// ```rust
/// // Taking a mutable reference.
/// fn push_two(v: &mut Vec<u8>) {
/// v.push(2);
/// }
///
/// // A mutable reference cannot be taken to a non-mutable variable.
/// let mut v = vec![0, 1];
/// // Passing a mutable reference.
/// push_two(&mut v);
///
/// assert_eq!(v, vec![0, 1, 2]);
/// ```
///
/// ```rust,compile_fail,E0502
/// let mut v = vec![0, 1];
/// let mut_ref_v = &mut v;
/// ##[allow(unused)]
/// let ref_v = &v;
/// mut_ref_v.push(2);
/// ```
///
/// 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/types/pointer.html#mutable-references-mut
mod mut_keyword {}

#[doc(keyword = "pub")]
Expand Down

0 comments on commit 9a5c4d7

Please sign in to comment.