Skip to content

Commit df8360f

Browse files
committed
Rollup merge of #24281 - steveklabnik:lol_editing, r=alexcrichton
more more more
2 parents 8766056 + 74b5c75 commit df8360f

File tree

9 files changed

+161
-311
lines changed

9 files changed

+161
-311
lines changed

src/doc/trpl/SUMMARY.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,39 @@
77
* [Learn Rust](learn-rust.md)
88
* [Effective Rust](effective-rust.md)
99
* [The Stack and the Heap](the-stack-and-the-heap.md)
10-
* [`Debug` and `Display`](debug-and-display.md)
10+
* [Debug and Display](debug-and-display.md)
1111
* [Testing](testing.md)
1212
* [Documentation](documentation.md)
1313
* [Iterators](iterators.md)
1414
* [Concurrency](concurrency.md)
1515
* [Error Handling](error-handling.md)
1616
* [FFI](ffi.md)
17-
* [`Deref` coercions](deref-coercions.md)
17+
* [Deref coercions](deref-coercions.md)
1818
* [Syntax and Semantics](syntax-and-semantics.md)
1919
* [Variable Bindings](variable-bindings.md)
2020
* [Functions](functions.md)
2121
* [Primitive Types](primitive-types.md)
2222
* [Comments](comments.md)
23-
* [Structs](structs.md)
24-
* [Mutability](mutability.md)
25-
* [Method Syntax](method-syntax.md)
26-
* [Enums](enums.md)
27-
* [`if`](if.md)
28-
* [Match](match.md)
29-
* [Patterns](patterns.md)
30-
* [`for` loops](for-loops.md)
31-
* [`while` loops](while-loops.md)
23+
* [if](if.md)
24+
* [for loops](for-loops.md)
25+
* [while loops](while-loops.md)
3226
* [Ownership](ownership.md)
3327
* [References and Borrowing](references-and-borrowing.md)
3428
* [Lifetimes](lifetimes.md)
29+
* [Mutability](mutability.md)
3530
* [Move semantics](move-semantics.md)
31+
* [Enums](enums.md)
32+
* [Match](match.md)
33+
* [Patterns](patterns.md)
34+
* [Structs](structs.md)
35+
* [Method Syntax](method-syntax.md)
3636
* [Drop](drop.md)
3737
* [Vectors](vectors.md)
3838
* [Strings](strings.md)
3939
* [Traits](traits.md)
4040
* [Operators and Overloading](operators-and-overloading.md)
4141
* [Generics](generics.md)
42+
* [if let](if-let.md)
4243
* [Trait Objects](trait-objects.md)
4344
* [Closures](closures.md)
4445
* [Universal Function Call Syntax](ufcs.md)
@@ -63,5 +64,6 @@
6364
* [Link args](link-args.md)
6465
* [Benchmark Tests](benchmark-tests.md)
6566
* [Box Syntax and Patterns](box-syntax-and-patterns.md)
67+
* [Slice Patterns](slice-patterns.md)
6668
* [Glossary](glossary.md)
6769
* [Academic Research](academic-research.md)

src/doc/trpl/debug-and-display.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
% `Debug` and `Display`
1+
% Debug and Display
22

33
Coming soon!

src/doc/trpl/for-loops.md

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
1-
% `for` Loops
1+
% for Loops
22

3-
The `for` loop is used to loop a particular number of times. Rust's `for` loops
4-
work a bit differently than in other systems languages, however. Rust's `for`
5-
loop doesn't look like this "C-style" `for` loop:
3+
The `for` loop is used to loop a particular number of times. Rusts `for` loops
4+
work a bit differently than in other systems languages, however. Rusts `for`
5+
loop doesnt look like this C-style `for` loop:
66

7-
```{c}
7+
```c
88
for (x = 0; x < 10; x++) {
99
printf( "%d\n", x );
1010
}
1111
```
1212

1313
Instead, it looks like this:
1414

15-
```{rust}
15+
```rust
1616
for x in 0..10 {
1717
println!("{}", x); // x: i32
1818
}
1919
```
2020

2121
In slightly more abstract terms,
2222

23-
```{ignore}
23+
```ignore
2424
for var in expression {
2525
code
2626
}
2727
```
2828

29-
The expression is an iterator, which we will discuss in more depth later in the
30-
guide. The iterator gives back a series of elements. Each element is one
31-
iteration of the loop. That value is then bound to the name `var`, which is
32-
valid for the loop body. Once the body is over, the next value is fetched from
33-
the iterator, and we loop another time. When there are no more values, the
34-
`for` loop is over.
29+
The expression is an [iterator][iterator]. The iterator gives back a series of
30+
elements. Each element is one iteration of the loop. That value is then bound
31+
to the name `var`, which is valid for the loop body. Once the body is over, the
32+
next value is fetched from the iterator, and we loop another time. When there
33+
are no more values, the `for` loop is over.
34+
35+
[iterator]: iterators.html
3536

3637
In our example, `0..10` is an expression that takes a start and an end position,
3738
and gives an iterator over those values. The upper bound is exclusive, though,
3839
so our loop will print `0` through `9`, not `10`.
3940

40-
Rust does not have the "C-style" `for` loop on purpose. Manually controlling
41+
Rust does not have the C-style `for` loop on purpose. Manually controlling
4142
each element of the loop is complicated and error prone, even for experienced C
4243
developers.
43-
44-
We'll talk more about `for` when we cover *iterators*, later in the Guide.

src/doc/trpl/if-let.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
% if let
2+
3+
COMING SOON

src/doc/trpl/if.md

+12-96
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
% `if`
1+
% if
22

3-
Rust's take on `if` is not particularly complex, but it's much more like the
4-
`if` you'll find in a dynamically typed language than in a more traditional
5-
systems language. So let's talk about it, to make sure you grasp the nuances.
3+
Rusts take on `if` is not particularly complex, but its much more like the
4+
`if` youll find in a dynamically typed language than in a more traditional
5+
systems language. So lets talk about it, to make sure you grasp the nuances.
66

7-
`if` is a specific form of a more general concept, the *branch*. The name comes
7+
`if` is a specific form of a more general concept, the branch. The name comes
88
from a branch in a tree: a decision point, where depending on a choice,
99
multiple paths can be taken.
1010

@@ -20,11 +20,11 @@ if x == 5 {
2020

2121
If we changed the value of `x` to something else, this line would not print.
2222
More specifically, if the expression after the `if` evaluates to `true`, then
23-
the block is executed. If it's `false`, then it is not.
23+
the block is executed. If its `false`, then it is not.
2424

2525
If you want something to happen in the `false` case, use an `else`:
2626

27-
```{rust}
27+
```rust
2828
let x = 5;
2929

3030
if x == 5 {
@@ -50,8 +50,7 @@ if x == 5 {
5050

5151
This is all pretty standard. However, you can also do this:
5252

53-
54-
```{rust}
53+
```rust
5554
let x = 5;
5655

5756
let y = if x == 5 {
@@ -63,95 +62,12 @@ let y = if x == 5 {
6362

6463
Which we can (and probably should) write like this:
6564

66-
```{rust}
65+
```rust
6766
let x = 5;
6867

6968
let y = if x == 5 { 10 } else { 15 }; // y: i32
7069
```
7170

72-
This reveals two interesting things about Rust: it is an expression-based
73-
language, and semicolons are different from semicolons in other 'curly brace
74-
and semicolon'-based languages. These two things are related.
75-
76-
## Expressions vs. Statements
77-
78-
Rust is primarily an expression based language. There are only two kinds of
79-
statements, and everything else is an expression.
80-
81-
So what's the difference? Expressions return a value, and statements do not.
82-
In many languages, `if` is a statement, and therefore, `let x = if ...` would
83-
make no sense. But in Rust, `if` is an expression, which means that it returns
84-
a value. We can then use this value to initialize the binding.
85-
86-
Speaking of which, bindings are a kind of the first of Rust's two statements.
87-
The proper name is a *declaration statement*. So far, `let` is the only kind
88-
of declaration statement we've seen. Let's talk about that some more.
89-
90-
In some languages, variable bindings can be written as expressions, not just
91-
statements. Like Ruby:
92-
93-
```{ruby}
94-
x = y = 5
95-
```
96-
97-
In Rust, however, using `let` to introduce a binding is _not_ an expression. The
98-
following will produce a compile-time error:
99-
100-
```{ignore}
101-
let x = (let y = 5); // expected identifier, found keyword `let`
102-
```
103-
104-
The compiler is telling us here that it was expecting to see the beginning of
105-
an expression, and a `let` can only begin a statement, not an expression.
106-
107-
Note that assigning to an already-bound variable (e.g. `y = 5`) is still an
108-
expression, although its value is not particularly useful. Unlike C, where an
109-
assignment evaluates to the assigned value (e.g. `5` in the previous example),
110-
in Rust the value of an assignment is the unit type `()` (which we'll cover later).
111-
112-
The second kind of statement in Rust is the *expression statement*. Its
113-
purpose is to turn any expression into a statement. In practical terms, Rust's
114-
grammar expects statements to follow other statements. This means that you use
115-
semicolons to separate expressions from each other. This means that Rust
116-
looks a lot like most other languages that require you to use semicolons
117-
at the end of every line, and you will see semicolons at the end of almost
118-
every line of Rust code you see.
119-
120-
What is this exception that makes us say "almost"? You saw it already, in this
121-
code:
122-
123-
```{rust}
124-
let x = 5;
125-
126-
let y: i32 = if x == 5 { 10 } else { 15 };
127-
```
128-
129-
Note that I've added the type annotation to `y`, to specify explicitly that I
130-
want `y` to be an integer.
131-
132-
This is not the same as this, which won't compile:
133-
134-
```{ignore}
135-
let x = 5;
136-
137-
let y: i32 = if x == 5 { 10; } else { 15; };
138-
```
139-
140-
Note the semicolons after the 10 and 15. Rust will give us the following error:
141-
142-
```text
143-
error: mismatched types: expected `i32`, found `()` (expected i32, found ())
144-
```
145-
146-
We expected an integer, but we got `()`. `()` is pronounced *unit*, and is a
147-
special type in Rust's type system. In Rust, `()` is _not_ a valid value for a
148-
variable of type `i32`. It's only a valid value for variables of the type `()`,
149-
which aren't very useful. Remember how we said statements don't return a value?
150-
Well, that's the purpose of unit in this case. The semicolon turns any
151-
expression into a statement by throwing away its value and returning unit
152-
instead.
153-
154-
There's one more time in which you won't see a semicolon at the end of a line
155-
of Rust code. For that, we'll need our next concept: functions.
156-
157-
TODO: `if let`
71+
This works because `if` is an expression. The value of the expression is the
72+
value of the last expression in whichever branch was chosen. An `if` without an
73+
`else` always results in `()` as the value.

0 commit comments

Comments
 (0)