Skip to content

Commit 8bf3072

Browse files
committed
Merge pull request #3652 from killerswan/review_tutorial
A run through the tutorial, small changes
2 parents 1c3bfa4 + 0b434af commit 8bf3072

File tree

1 file changed

+58
-42
lines changed

1 file changed

+58
-42
lines changed

doc/tutorial.md

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ example, by changing `io::println` to some nonexistent function), and
152152
then compile it, you'll see an error message like this:
153153

154154
~~~~ {.notrust}
155-
hello.rs:2:4: 2:16 error: unresolved name: io::print_it
156-
hello.rs:2 io::print_it("hello? yes, this is rust");
157-
^~~~~~~~~~~~
155+
hello.rs:2:4: 2:16 error: unresolved name: io::print_with_unicorns
156+
hello.rs:2 io::print_with_unicorns("hello? yes, this is rust");
157+
^~~~~~~~~~~~~~~~~~~~~~~
158158
~~~~
159159

160160
In its simplest form, a Rust program is a `.rs` file with some types
@@ -178,9 +178,11 @@ included in that directory. In particular, if you are running emacs
178178
24, then using emacs's internal package manager to install `rust-mode`
179179
is the easiest way to keep it up to date. There is also a package for
180180
Sublime Text 2, available both [standalone][sublime] and through
181-
[Sublime Package Control][sublime-pkg].
181+
[Sublime Package Control][sublime-pkg], and support for Kate
182+
under `src/etc/kate`.
182183

183-
Other editors are not provided for yet. If you end up writing a Rust
184+
There is ctags support via `src/etc/ctags.rust`, but many other
185+
tools and editors are not provided for yet. If you end up writing a Rust
184186
mode for your favorite editor, let us know so that we can link to it.
185187

186188
[sublime]: http://github.com/dbp/sublime-rust
@@ -191,7 +193,7 @@ mode for your favorite editor, let us know so that we can link to it.
191193
Assuming you've programmed in any C-family language (C++, Java,
192194
JavaScript, C#, or PHP), Rust will feel familiar. Code is arranged
193195
in blocks delineated by curly braces; there are control structures
194-
for branching and looping, like the familiar `if` and `when`; function
196+
for branching and looping, like the familiar `if` and `while`; function
195197
calls are written `myfunc(arg1, arg2)`; operators are written the same
196198
and mostly have the same precedence as in C; comments are again like C.
197199

@@ -227,13 +229,14 @@ while count < 10 {
227229
}
228230
~~~~
229231

230-
Although Rust can almost always infer the types of local variables, it
231-
can help readability to specify a variable's type by following it with
232-
a colon, then the type name.
232+
Although Rust can almost always infer the types of local variables, you
233+
can specify a variable's type by following it with a colon, then the type
234+
name.
233235

234236
~~~~
235-
let my_favorite_value: float = 57.8;
236-
let my_favorite_value: int = my_favorite_value as int;
237+
let monster_size: float = 57.8;
238+
let imaginary_size = monster_size * 10;
239+
let monster_size: int = 50;
237240
~~~~
238241

239242
Local variables may shadow earlier declarations, as in the previous
@@ -248,14 +251,14 @@ underscores where they help readability, while writing types in camel case.
248251

249252
~~~
250253
let my_variable = 100;
251-
type MyType = int; // built-in types though are _not_ camel case
254+
type MyType = int; // some built-in types are _not_ camel case
252255
~~~
253256

254257
## Expression syntax
255258

256259
Though it isn't apparent in all code, there is a fundamental
257-
difference between Rust's syntax and its predecessors in this family
258-
of languages. Many constructs that are statements in C are expressions
260+
difference between Rust's syntax and predecessors like C.
261+
Many constructs that are statements in C are expressions
259262
in Rust, allowing code to be more concise. For example, you might
260263
write a piece of code like this:
261264

@@ -275,24 +278,25 @@ But, in Rust, you don't have to repeat the name `price`:
275278

276279
~~~~
277280
# let item = "salad";
278-
let price = if item == "salad" {
279-
3.50
280-
} else if item == "muffin" {
281-
2.25
282-
} else {
283-
2.00
284-
};
281+
let price =
282+
if item == "salad" {
283+
3.50
284+
} else if item == "muffin" {
285+
2.25
286+
} else {
287+
2.00
288+
};
285289
~~~~
286290

287291
Both pieces of code are exactly equivalent—they assign a value to
288-
`price` depending on the condition that holds. Note that the
289-
semicolons are omitted from the blocks in the second snippet. This is
292+
`price` depending on the condition that holds. Note that there
293+
are not semicolons in the blocks of the second snippet. This is
290294
important; the lack of a semicolon after the last statement in a
291295
braced block gives the whole block the value of that last expression.
292296

293297
Put another way, the semicolon in Rust *ignores the value of an expression*.
294298
Thus, if the branches of the `if` had looked like `{ 4; }`, the above example
295-
would simply assign nil (void) to `price`. But without the semicolon, each
299+
would simply assign `()` (nil or void) to `price`. But without the semicolon, each
296300
branch has a different value, and `price` gets the value of the branch that
297301
was taken.
298302

@@ -346,8 +350,7 @@ if x {
346350
let y = if x { foo() } else { bar() };
347351
~~~
348352

349-
This may sound a bit intricate, but it is super-useful, and it will
350-
grow on you (hopefully).
353+
This may sound a intricate, but it is super-useful and will grow on you.
351354

352355
## Types
353356

@@ -365,7 +368,8 @@ The basic types include the usual boolean, integral, and floating point types.
365368
------------------------- -----------------------------------------------
366369

367370
These can be combined in composite types, which will be described in
368-
more detail later on (the `T`s here stand for any other type):
371+
more detail later on (the `T`s here stand for any other type,
372+
while N should be a literal number):
369373

370374
------------------------- -----------------------------------------------
371375
`[T * N]` Vector (like an array in other languages) with N elements
@@ -392,7 +396,7 @@ the type `fn() -> bool` or the function declaration `fn foo() -> bool
392396
optionally write `-> ()`, but usually the return annotation is simply
393397
left off, as in `fn main() { ... }`.
394398

395-
Types can be given names with `type` declarations:
399+
Types can be given names or aliases with `type` declarations:
396400

397401
~~~~
398402
type MonsterSize = uint;
@@ -401,9 +405,25 @@ type MonsterSize = uint;
401405
This will provide a synonym, `MonsterSize`, for unsigned integers. It will not
402406
actually create a new, incompatible type—`MonsterSize` and `uint` can be used
403407
interchangeably, and using one where the other is expected is not a type
404-
error. Read about [single-variant enums](#single_variant_enum)
405-
further on if you need to create a type name that's not just a
406-
synonym.
408+
error.
409+
410+
To create data types which are not synonyms, `struct` and `enum`
411+
can be used. They're described in more detail below, but they look like this:
412+
413+
~~~~
414+
enum HidingPlaces {
415+
Closet(uint),
416+
UnderTheBed(uint)
417+
}
418+
419+
struct HeroicBabysitter {
420+
bedtime_stories: uint,
421+
sharpened_stakes: uint
422+
}
423+
424+
struct BabysitterSize(uint); // a single-variant struct
425+
enum MonsterSize = uint; // a single-variant enum
426+
~~~~
407427

408428
## Literals
409429

@@ -435,7 +455,7 @@ The nil literal is written just like the type: `()`. The keywords
435455

436456
Character literals are written between single quotes, as in `'x'`. Just as in
437457
C, Rust understands a number of character escapes, using the backslash
438-
character, `\n`, `\r`, and `\t` being the most common. String literals,
458+
character, such as `\n`, `\r`, and `\t`. String literals,
439459
written between double quotes, allow the same escape sequences. Rust strings
440460
may contain newlines.
441461

@@ -466,8 +486,8 @@ assert y == 4u;
466486

467487
The main difference with C is that `++` and `--` are missing, and that
468488
the logical bitwise operators have higher precedence — in C, `x & 2 > 0`
469-
comes out as `x & (2 > 0)`, in Rust, it means `(x & 2) > 0`, which is
470-
more likely to be what you expect (unless you are a C veteran).
489+
means `x & (2 > 0)`, but in Rust, it means `(x & 2) > 0`, which is
490+
more likely what a novice expects.
471491

472492
## Syntax extensions
473493

@@ -485,7 +505,7 @@ don't match the types of the arguments.
485505
~~~~
486506
# let mystery_object = ();
487507
488-
io::println(fmt!("%s is %d", "the answer", 43));
508+
io::println(fmt!("%s is %d", "the answer", 42));
489509
490510
// %? will conveniently print any type
491511
io::println(fmt!("what is this thing: %?", mystery_object));
@@ -556,18 +576,14 @@ underscore (`_`) is a wildcard pattern that matches everything.
556576

557577
The patterns in an match arm are followed by a fat arrow, `=>`, then an
558578
expression to evaluate. Each case is separated by commas. It's often
559-
convenient to use a block expression for a case, in which case the
579+
convenient to use a block expression for each case, in which case the
560580
commas are optional.
561581

562582
~~~
563583
# let my_number = 1;
564584
match my_number {
565-
0 => {
566-
io::println("zero")
567-
}
568-
_ => {
569-
io::println("something else")
570-
}
585+
0 => { io::println("zero") }
586+
_ => { io::println("something else") }
571587
}
572588
~~~
573589

0 commit comments

Comments
 (0)