about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSteve Klabnik <steve@steveklabnik.com>2014-07-02 13:21:58 -0400
committerAlex Crichton <alex@alexcrichton.com>2014-07-03 12:54:51 -0700
commit9868b65b153a5bed9ca75eca750efae13e93cc44 (patch)
treeed917210148972943329d926ab020c48a3f91841
parente38cb972dcfc0fdab44270257eac3405a39bd996 (diff)
downloadrust-9868b65b153a5bed9ca75eca750efae13e93cc44.tar.gz
rust-9868b65b153a5bed9ca75eca750efae13e93cc44.zip
Guide: add mutable binding section
Fixes #15334.
-rw-r--r--src/doc/guide.md42
1 files changed, 39 insertions, 3 deletions
diff --git a/src/doc/guide.md b/src/doc/guide.md
index 7ecfe7ce578..aca726812d2 100644
--- a/src/doc/guide.md
+++ b/src/doc/guide.md
@@ -515,9 +515,45 @@ let x: int = 5;
 ```
 
 If I asked you to read this out loud to the rest of the class, you'd say "`x`
-is a binding with the type `int` and the value `five`." Rust requires you to
-initialize the binding with a value before you're allowed to use it. If
-we try...
+is a binding with the type `int` and the value `five`."
+
+By default, bindings are **immutable**. This code will not compile:
+
+```{ignore}
+let x = 5i;
+x = 10i;
+```
+
+It will give you this error:
+
+```{ignore,notrust}
+error: re-assignment of immutable variable `x`
+     x = 10i;
+     ^~~~~~~
+```
+
+If you want a binding to be mutable, you can use `mut`:
+
+```{rust}
+let mut x = 5i;
+x = 10i;
+```
+
+There is no single reason that bindings are immutable by default, but we can
+think about it through one of Rust's primary focuses: safety. If you forget to
+say `mut`, the compiler will catch it, and let you know that you have mutated
+something you may not have cared to mutate. If bindings were mutable by
+default, the compiler would not be able to tell you this. If you _did_ intend
+mutation, then the solution is quite easy: add `mut`.
+
+There are other good reasons to avoid mutable state when possible, but they're
+out of the scope of this guide. In general, you can often avoid explicit
+mutation, and so it is preferable in Rust. That said, sometimes, mutation is
+what you need, so it's not verboten.
+
+Let's get back to bindings. Rust variable bindings have one more aspect that
+differs from other languages: bindings are required to be initialized with a
+value before you're allowed to use it. If we try...
 
 ```{ignore}
 let x;