Skip to content

Commit 3d09017

Browse files
committed
Add a compile fail example, binding -> variable, apply suggestions
1 parent 3c46e36 commit 3d09017

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/libstd/keyword_docs.rs

+22-13
Original file line numberDiff line numberDiff line change
@@ -965,49 +965,58 @@ mod move_keyword {}
965965

966966
#[doc(keyword = "mut")]
967967
//
968-
/// A mutable binding, reference, or pointer.
968+
/// A mutable variable, reference, or pointer.
969969
///
970-
/// `mut` can be used in several situations. The first is mutable bindings,
970+
/// `mut` can be used in several situations. The first is mutable variables,
971971
/// which can be used anywhere you can bind a value to a variable name. Some
972972
/// examples:
973973
///
974-
/// ```
975-
/// // A mutable binding in the parameter list of a function.
974+
/// ```rust
975+
/// // A mutable variable in the parameter list of a function.
976976
/// fn foo(mut x: u8, y: u8) -> u8 {
977977
/// x += y;
978978
/// x
979979
/// }
980980
///
981-
/// // A mutable binding for a variable.
981+
/// // Modifying a mutable variable.
982+
/// # #[allow(unused_assignments)]
982983
/// let mut a = 5;
983984
/// a = 6;
984985
///
985986
/// assert_eq!(foo(3, 4), 7);
986987
/// assert_eq!(a, 6);
987988
/// ```
988989
///
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.
990+
/// The second is mutable references. They can be created from `mut` variables
991+
/// and must be unique: no other variables can have a mutable reference, nor a
992+
/// shared reference.
992993
///
993-
/// ```
994+
/// ```rust
994995
/// // Taking a mutable reference.
995996
/// fn push_two(v: &mut Vec<u8>) {
996997
/// v.push(2);
997998
/// }
998999
///
999-
/// // You cannot take a mutable reference to a non-mutable variable.
1000+
/// // A mutable reference cannot be taken to a non-mutable variable.
10001001
/// let mut v = vec![0, 1];
10011002
/// // Passing a mutable reference.
10021003
/// push_two(&mut v);
10031004
///
10041005
/// assert_eq!(v, vec![0, 1, 2]);
10051006
/// ```
10061007
///
1007-
/// Mutable pointers work much like mutable references, with the added
1008-
/// possibility of being nul. The syntax is `*mut Type`.
1008+
/// ```rust,compile_fail,E0502
1009+
/// let mut v = vec![0, 1];
1010+
/// let mut_ref_v = &mut v;
1011+
/// ##[allow(unused)]
1012+
/// let ref_v = &v;
1013+
/// mut_ref_v.push(2);
1014+
/// ```
1015+
///
1016+
/// Mutable raw pointers work much like mutable references, with the added
1017+
/// possibility of not pointing to a valid object. The syntax is `*mut Type`.
10091018
///
1010-
/// You can find more information on mutable references and pointers in the
1019+
/// More information on mutable references and pointers can be found in```
10111020
/// [Reference].
10121021
///
10131022
/// [Reference]: ../reference/types/pointer.html#mutable-references-mut

0 commit comments

Comments
 (0)