Skip to content

Commit bfdc537

Browse files
steveklabnikalexcrichton
authored andcommitted
---
yaml --- r: 131481 b: refs/heads/dist-snap c: 9868b65 h: refs/heads/master i: 131479: f11f4f6 v: v3
1 parent 99d9516 commit bfdc537

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: 457a3c991d79b971be07fce75f9d0c12848fb37c
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
9-
refs/heads/dist-snap: e38cb972dcfc0fdab44270257eac3405a39bd996
9+
refs/heads/dist-snap: 9868b65b153a5bed9ca75eca750efae13e93cc44
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1212
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/src/doc/guide.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,45 @@ let x: int = 5;
515515
```
516516

517517
If I asked you to read this out loud to the rest of the class, you'd say "`x`
518-
is a binding with the type `int` and the value `five`." Rust requires you to
519-
initialize the binding with a value before you're allowed to use it. If
520-
we try...
518+
is a binding with the type `int` and the value `five`."
519+
520+
By default, bindings are **immutable**. This code will not compile:
521+
522+
```{ignore}
523+
let x = 5i;
524+
x = 10i;
525+
```
526+
527+
It will give you this error:
528+
529+
```{ignore,notrust}
530+
error: re-assignment of immutable variable `x`
531+
x = 10i;
532+
^~~~~~~
533+
```
534+
535+
If you want a binding to be mutable, you can use `mut`:
536+
537+
```{rust}
538+
let mut x = 5i;
539+
x = 10i;
540+
```
541+
542+
There is no single reason that bindings are immutable by default, but we can
543+
think about it through one of Rust's primary focuses: safety. If you forget to
544+
say `mut`, the compiler will catch it, and let you know that you have mutated
545+
something you may not have cared to mutate. If bindings were mutable by
546+
default, the compiler would not be able to tell you this. If you _did_ intend
547+
mutation, then the solution is quite easy: add `mut`.
548+
549+
There are other good reasons to avoid mutable state when possible, but they're
550+
out of the scope of this guide. In general, you can often avoid explicit
551+
mutation, and so it is preferable in Rust. That said, sometimes, mutation is
552+
what you need, so it's not verboten.
553+
554+
Let's get back to bindings. Rust variable bindings have one more aspect that
555+
differs from other languages: bindings are required to be initialized with a
556+
value before you're allowed to use it. If we try...
521557

522558
```{ignore}
523559
let x;

0 commit comments

Comments
 (0)