Skip to content

Commit 73ff6aa

Browse files
steveklabnikalexcrichton
authored andcommitted
---
yaml --- r: 153059 b: refs/heads/try2 c: 9868b65 h: refs/heads/master i: 153057: c3fe88f 153055: 27a3209 v: v3
1 parent d0809ee commit 73ff6aa

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
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: e38cb972dcfc0fdab44270257eac3405a39bd996
8+
refs/heads/try2: 9868b65b153a5bed9ca75eca750efae13e93cc44
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/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)