Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
549bd55
resurrect research paper list #24004
Apr 9, 2015
c3aa057
Add regression test for #19097
aturon Apr 9, 2015
644a75e
Fix `borrow` docs
apasel422 Apr 9, 2015
69f63e9
Indicate keyword in doc comment is code-like
frewsxcv Apr 9, 2015
8578fee
Don't use skolemized parameters but rather fresh variables in
nikomatsakis Apr 9, 2015
9eb8528
Copyediting for 'variable bindings'
steveklabnik Apr 9, 2015
bf88539
TRPL: new introduction
steveklabnik Apr 9, 2015
06dc26d
More editing work on TRPL
steveklabnik Apr 9, 2015
7bb0cd7
Write the 'primitive types' section of TRPL
steveklabnik Apr 9, 2015
2a88b79
Add tests for E-needstest issues
lgrz Apr 10, 2015
16574e3
Replace the use of the rather randomly named boolean `custom` to mean
nikomatsakis Apr 9, 2015
5156b3a
Modify the codemap code to use more slices and to information about
nikomatsakis Apr 9, 2015
906a972
Add a new `span_suggestion` infrastructure. This lets you edit a snippet
nikomatsakis Apr 9, 2015
e313b33
Improve error message where a closure escapes fn while trying to borrow
nikomatsakis Apr 9, 2015
e66569e
Fix pow docs to not use Int
steveklabnik Apr 10, 2015
cdce32f
Changed the wording of the documentation for the insert method for Ve…
djallen89 Apr 10, 2015
c2fa1f7
Doc: remove a "safety note" made obsolete by dropck for TypedArena
SimonSapin Apr 10, 2015
7bf1da1
s/Panicks/Panics/
apasel422 Apr 10, 2015
288b1c9
Add examples for Convert
steveklabnik Apr 6, 2015
9e68d23
Fix mistake in documentation
xamgore Apr 10, 2015
59ebc20
Rollup merge of #24121 - steveklabnik:gh24107, r=steveklabnik
steveklabnik Apr 10, 2015
7b415a9
Rollup merge of #24234 - thiagooak:academic-research, r=steveklabnik
steveklabnik Apr 10, 2015
4801260
Rollup merge of #24236 - aturon:issue-19097, r=alexcrichton
steveklabnik Apr 10, 2015
7725c8d
Rollup merge of #24239 - steveklabnik:editing_pass, r=steveklabnik
steveklabnik Apr 10, 2015
cadfcc5
Rollup merge of #24240 - apasel422:patch-1, r=aturon
steveklabnik Apr 10, 2015
29e110e
Rollup merge of #24242 - nikomatsakis:escaping-closure-error-message,…
steveklabnik Apr 10, 2015
bb2cdc1
Rollup merge of #24243 - frewsxcv:patch-13, r=steveklabnik
steveklabnik Apr 10, 2015
ede2761
Rollup merge of #24244 - steveklabnik:more_editing, r=steveklabnik
steveklabnik Apr 10, 2015
2dd17d7
Rollup merge of #24245 - nikomatsakis:issue-24241-coherence-failure, …
steveklabnik Apr 10, 2015
c381844
Rollup merge of #24247 - steveklabnik:update_variable_bindings, r=huonw
steveklabnik Apr 10, 2015
4aea07a
Rollup merge of #24253 - steveklabnik:doc_primitive_types, r=alexcric…
steveklabnik Apr 10, 2015
bbec378
Rollup merge of #24259 - lstat:needstest, r=alexcrichton
steveklabnik Apr 10, 2015
f940465
Rollup merge of #24274 - steveklabnik:fix_pow_docs, r=nikomatsakis
steveklabnik Apr 10, 2015
69ae855
Rollup merge of #24279 - libfud:vec_insertion_docs, r=steveklabnik
steveklabnik Apr 10, 2015
019af46
Rollup merge of #24282 - SimonSapin:patch-6, r=pnkfelix
steveklabnik Apr 10, 2015
c9976a1
Rollup merge of #24283 - apasel422:patch-2, r=alexcrichton
steveklabnik Apr 10, 2015
fbe2ec1
Rollup merge of #24291 - xamgore:patch-1, r=steveklabnik
steveklabnik Apr 10, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Copyediting for 'variable bindings'
  • Loading branch information
steveklabnik committed Apr 9, 2015
commit 9eb85288c183b2d9e256adcdcfdad0df03f91de6
106 changes: 57 additions & 49 deletions src/doc/trpl/variable-bindings.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
% Variable Bindings

The first thing we'll learn about are *variable bindings*. They look like this:
Vitually every non-’Hello World’ Rust program uses *variable bindings*. They
look like this:

```{rust}
```rust
fn main() {
let x = 5;
}
```

Putting `fn main() {` in each example is a bit tedious, so we'll leave that out
in the future. If you're following along, make sure to edit your `main()`
function, rather than leaving it off. Otherwise, you'll get an error.
Putting `fn main() {` in each example is a bit tedious, so well leave that out
in the future. If youre following along, make sure to edit your `main()`
function, rather than leaving it off. Otherwise, youll get an error.

In many languages, this is called a *variable*. But Rust's variable bindings
have a few tricks up their sleeves. Rust has a very powerful feature called
*pattern matching* that we'll get into detail with later, but the left
hand side of a `let` expression is a full pattern, not just a variable name.
This means we can do things like:
In many languages, this is called a *variable*, but Rust’s variable bindings
have a few tricks up their sleeves. For example the left-hand side of a `let`
expression is a ‘[pattern][pattern]’, not just a variable name. This means we
can do things like:

```{rust}
```rust
let (x, y) = (1, 2);
```

After this expression is evaluated, `x` will be one, and `y` will be two.
Patterns are really powerful, but this is about all we can do with them so far.
So let's just keep this in the back of our minds as we go forward.
Patterns are really powerful, and have [their own section][pattern] in the
book. We don’t need those features for now, so we’ll just keep this in the back
of our minds as we go forward.

[pattern]: patterns.html

Rust is a statically typed language, which means that we specify our types up
front. So why does our first example compile? Well, Rust has this thing called
*type inference*. If it can figure out what the type of something is, Rust
doesn't require you to actually type it out.
front, and they’re checked at compile time. So why does our first example
compile? Well, Rust has this thing called ‘type inference’. If it can figure
out what the type of something is, Rust doesn’t require you to actually type it
out.

We can add the type if we want to, though. Types come after a colon (`:`):

```{rust}
```rust
let x: i32 = 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 `i32` and the value `five`."
If I asked you to read this out loud to the rest of the class, youd say `x`
is a binding with the type `i32` and the value `five`.

In this case we chose to represent `x` as a 32-bit signed integer. Rust has
many different primitive integer types. They begin with `i` for signed integers
Expand All @@ -48,19 +52,20 @@ bits.
In future examples, we may annotate the type in a comment. The examples will
look like this:

```{rust}
```rust
fn main() {
let x = 5; // x: i32
}
```

Note the similarities between this annotation and the syntax you use with `let`.
Including these kinds of comments is not idiomatic Rust, but we'll occasionally
include them to help you understand what the types that Rust infers are.
Note the similarities between this annotation and the syntax you use with
`let`. Including these kinds of comments is not idiomatic Rust, but we'll
occasionally include them to help you understand what the types that Rust
infers are.

By default, bindings are *immutable*. This code will not compile:

```{ignore}
```rust,ignore
let x = 5;
x = 10;
```
Expand All @@ -75,62 +80,63 @@ error: re-assignment of immutable variable `x`

If you want a binding to be mutable, you can use `mut`:

```{rust}
```rust
let mut x = 5; // mut x: i32
x = 10;
```

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
think about it through one of Rusts 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 intended 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
There are other good reasons to avoid mutable state when possible, but theyre
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.
what you need, so its not verboten.

Let's get back to bindings. Rust variable bindings have one more aspect that
Lets 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 them.

Let's try it out. Change your `src/main.rs` file to look like this:
Lets try it out. Change your `src/main.rs` file to look like this:

```{rust}
```rust
fn main() {
let x: i32;

println!("Hello world!");
}
```

You can use `cargo build` on the command line to build it. You'll get a warning,
but it will still print "Hello, world!":
You can use `cargo build` on the command line to build it. Youll get a
warning, but it will still print "Hello, world!":

```text
Compiling hello_world v0.0.1 (file:///home/you/projects/hello_world)
src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variable)] on by default
src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variable)]
on by default
src/main.rs:2 let x: i32;
^
```

Rust warns us that we never use the variable binding, but since we never use it,
no harm, no foul. Things change if we try to actually use this `x`, however. Let's
do that. Change your program to look like this:
Rust warns us that we never use the variable binding, but since we never use
it, no harm, no foul. Things change if we try to actually use this `x`,
however. Let’s do that. Change your program to look like this:

```{rust,ignore}
```rust,ignore
fn main() {
let x: i32;

println!("The value of x is: {}", x);
}
```

And try to build it. You'll get an error:
And try to build it. Youll get an error:

```{bash}
```bash
$ cargo build
Compiling hello_world v0.0.1 (file:///home/you/projects/hello_world)
src/main.rs:4:39: 4:40 error: use of possibly uninitialized variable: `x`
Expand All @@ -144,18 +150,20 @@ error: aborting due to previous error
Could not compile `hello_world`.
```

Rust will not let us use a value that has not been initialized. Next, let's
Rust will not let us use a value that has not been initialized. Next, lets
talk about this stuff we've added to `println!`.

If you include two curly braces (`{}`, some call them moustaches...) in your
string to print, Rust will interpret this as a request to interpolate some sort
of value. *String interpolation* is a computer science term that means "stick
in the middle of a string." We add a comma, and then `x`, to indicate that we
want `x` to be the value we're interpolating. The comma is used to separate
arguments we pass to functions and macros, if you're passing more than one.

When you just use the curly braces, Rust will attempt to display the
value in a meaningful way by checking out its type. If you want to specify the
format in a more detailed manner, there are a [wide number of options
available](../std/fmt/index.html). For now, we'll just stick to the default:
integers aren't very complicated to print.
want `x` to be the value we’re interpolating. The comma is used to separate
arguments we pass to functions and macros, if you’re passing more than one.

When you just use the curly braces, Rust will attempt to display the value in a
meaningful way by checking out its type. If you want to specify the format in a
more detailed manner, there are a [wide number of options available][format].
For now, we'll just stick to the default: integers aren't very complicated to
print.

[format]: ../std/fmt/index.html