Skip to content

Commit 8b57cb9

Browse files
committed
Clarify tutorial based on feedback, fix some Markdown errors
1 parent ca7d750 commit 8b57cb9

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

doc/tutorial/control.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ extract the fields from a tuple:
9494
This will introduce two new variables, `a` and `b`, bound to the
9595
content of the tuple.
9696

97-
You may only use irrevocable patterns in let bindings, though. Things
98-
like literals, which only match a specific value, are not allowed.
97+
You may only use irrevocable patterns—patterns that can never fail to
98+
match—in let bindings, though. Things like literals, which only match
99+
a specific value, are not allowed.
99100

100101
## Loops
101102

@@ -114,6 +115,13 @@ to abort the current iteration and continue with the next.
114115
This code prints out a weird sequence of numbers and stops as soon as
115116
it finds one that can be divided by five.
116117

118+
There's also `while`'s ugly cousin, `do`/`while`, which does not check
119+
its condition on the first iteration, using traditional syntax:
120+
121+
do {
122+
eat_cake();
123+
} while any_cake_left();
124+
117125
When iterating over a vector, use `for` instead.
118126

119127
for elt in ["red", "green", "blue"] {
@@ -167,3 +175,14 @@ leave them in.
167175

168176
For interactive debugging, you often want unconditional logging. For
169177
this, use `log_err` instead of `log` [FIXME better name].
178+
179+
## Assertions
180+
181+
The keyword `assert`, followed by an expression with boolean type,
182+
will check that the given expression results in `true`, and cause a
183+
failure otherwise. It is typically used to double-check things that
184+
*should* hold at a certain point in a program.
185+
186+
let x = 100;
187+
while (x > 10) { x -= 10; }
188+
assert x == 10;

doc/tutorial/data.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ error.
3333
To 'update' an immutable record, you use functional record update
3434
syntax, by ending a record literal with the keyword `with`:
3535

36+
let oldpoint = {x: 10f, y: 20f};
3637
let newpoint = {x: 0f with oldpoint};
38+
assert newpoint == {x: 0f, y: 20f};
3739

3840
This will create a new struct, copying all the fields from `oldpoint`
3941
into it, except for the ones that are explicitly set in the literal.

doc/tutorial/setup.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ FIXME say something about libs, main, modules, use
3636

3737
There are Vim highlighting and indentation scrips in the Rust source
3838
distribution under `src/etc/vim/`. An Emacs mode can be found at
39-
`[https://github.com/marijnh/rust-mode](https://github.com/marijnh/rust-mode)`.
39+
[https://github.com/marijnh/rust-mode][rust-mode].
40+
41+
[rust-mode]: https://github.com/marijnh/rust-mode
4042

4143
Other editors are not provided for yet. If you end up writing a Rust
4244
mode for your favorite editor, let us know so that we can link to it.

doc/tutorial/syntax.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ like `if` and `while` are available:
3737

3838
Though it isn't apparent in most everyday code, there is a fundamental
3939
difference between Rust's syntax and the predecessors in this family
40-
of languages. Almost everything in rust is an expression, even things
41-
that are statements in other languages. This allows for useless things
42-
like this (which passes nil—the void type—to a function):
40+
of languages. A lot of thing that are statements in C are expressions
41+
in Rust. This allows for useless things like this (which passes
42+
nil—the void type—to a function):
4343

4444
a_function(while false {});
4545

@@ -62,11 +62,14 @@ This also works for function bodies. This function returns a boolean:
6262

6363
fn is_four(x: int) -> bool { x == 4 }
6464

65-
If everything is an expression, you might conclude that you have to
66-
add a terminating semicolon after *every* statement, even ones that
65+
In short, everything that's not a declaration (`let` for variables,
66+
`fn` for functions, etcetera) is an expression.
67+
68+
If all those things are expressions, you might conclude that you have
69+
to add a terminating semicolon after *every* statement, even ones that
6770
are not traditionally terminated with a semicolon in C (like `while`).
68-
That is not the case, though. Statements that end in a block only need
69-
a semicolon if that block contains a trailing expression. `while`
71+
That is not the case, though. Expressions that end in a block only
72+
need a semicolon if that block contains a trailing expression. `while`
7073
loops do not allow trailing expressions, and `if` statements tend to
7174
only have a trailing expression when you want to use their value for
7275
something—in which case you'll have embedded it in a bigger statement,
@@ -166,7 +169,7 @@ Integers can be written in decimal (`144`), hexadecimal (`0x90`), and
166169
binary (`0b10010000`) base. Without suffix, an integer literal is
167170
considered to be of type `int`. Add a `u` (`144u`) to make it a `uint`
168171
instead. Literals of the fixed-size integer types can be created by
169-
the literal with the type name (`i8`, `u64`, etc).
172+
the literal with the type name (`255u8`, `50i64`, etc).
170173

171174
Note that, in Rust, no implicit conversion between integer types
172175
happens. If you are adding one to a variable of type `uint`, you must
@@ -188,12 +191,16 @@ character escapes, using the backslash character:
188191

189192
`\n`
190193
: A newline (unicode character 32).
194+
191195
`\r`
192196
: A carriage return (13).
197+
193198
`\t`
194199
: A tab character (9).
200+
195201
`\\`, `\'`, `\"`
196202
: Simply escapes the following character.
203+
197204
`\xHH`, `\uHHHH`, `\UHHHHHHHH`
198205
: Unicode escapes, where the `H` characters are the hexadecimal digits that form the character code.
199206

0 commit comments

Comments
 (0)