Skip to content

Commit 3c46e36

Browse files
committed
Document the mut keyword
1 parent 1a4e2b6 commit 3c46e36

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/libstd/keyword_docs.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,9 +967,50 @@ mod move_keyword {}
967967
//
968968
/// A mutable binding, reference, or pointer.
969969
///
970-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
970+
/// `mut` can be used in several situations. The first is mutable bindings,
971+
/// which can be used anywhere you can bind a value to a variable name. Some
972+
/// examples:
971973
///
972-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
974+
/// ```
975+
/// // A mutable binding in the parameter list of a function.
976+
/// fn foo(mut x: u8, y: u8) -> u8 {
977+
/// x += y;
978+
/// x
979+
/// }
980+
///
981+
/// // A mutable binding for a variable.
982+
/// let mut a = 5;
983+
/// a = 6;
984+
///
985+
/// assert_eq!(foo(3, 4), 7);
986+
/// assert_eq!(a, 6);
987+
/// ```
988+
///
989+
/// The second is references. They can be created from `mut` bindings and must
990+
/// be unique: no other binding can have a mutable reference, nor a simple
991+
/// reference.
992+
///
993+
/// ```
994+
/// // Taking a mutable reference.
995+
/// fn push_two(v: &mut Vec<u8>) {
996+
/// v.push(2);
997+
/// }
998+
///
999+
/// // You cannot take a mutable reference to a non-mutable variable.
1000+
/// let mut v = vec![0, 1];
1001+
/// // Passing a mutable reference.
1002+
/// push_two(&mut v);
1003+
///
1004+
/// assert_eq!(v, vec![0, 1, 2]);
1005+
/// ```
1006+
///
1007+
/// Mutable pointers work much like mutable references, with the added
1008+
/// possibility of being nul. The syntax is `*mut Type`.
1009+
///
1010+
/// You can find more information on mutable references and pointers in the
1011+
/// [Reference].
1012+
///
1013+
/// [Reference]: ../reference/types/pointer.html#mutable-references-mut
9731014
mod mut_keyword {}
9741015

9751016
#[doc(keyword = "pub")]

0 commit comments

Comments
 (0)