Skip to content

Commit 34a8d93

Browse files
committed
---
yaml --- r: 30990 b: refs/heads/incoming c: 82fd711 h: refs/heads/master v: v3
1 parent 3165fb8 commit 34a8d93

File tree

4 files changed

+156
-196
lines changed

4 files changed

+156
-196
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
9-
refs/heads/incoming: 0b434af7ebefdc82dfa58396b374970d1bbb5a7d
9+
refs/heads/incoming: 82fd71137a87ad4568eee24142d22bf87a4d2e2b
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/doc/tutorial.md

Lines changed: 42 additions & 58 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_with_unicorns
156-
hello.rs:2 io::print_with_unicorns("hello? yes, this is rust");
157-
^~~~~~~~~~~~~~~~~~~~~~~
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+
^~~~~~~~~~~~
158158
~~~~
159159

160160
In its simplest form, a Rust program is a `.rs` file with some types
@@ -178,11 +178,9 @@ 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], and support for Kate
182-
under `src/etc/kate`.
181+
[Sublime Package Control][sublime-pkg].
183182

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
183+
Other editors are not provided for yet. If you end up writing a Rust
186184
mode for your favorite editor, let us know so that we can link to it.
187185

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

@@ -229,14 +227,13 @@ while count < 10 {
229227
}
230228
~~~~
231229

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.
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.
235233

236234
~~~~
237-
let monster_size: float = 57.8;
238-
let imaginary_size = monster_size * 10;
239-
let monster_size: int = 50;
235+
let my_favorite_value: float = 57.8;
236+
let my_favorite_value: int = my_favorite_value as int;
240237
~~~~
241238

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

252249
~~~
253250
let my_variable = 100;
254-
type MyType = int; // some built-in types are _not_ camel case
251+
type MyType = int; // built-in types though are _not_ camel case
255252
~~~
256253

257254
## Expression syntax
258255

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

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

279276
~~~~
280277
# let item = "salad";
281-
let price =
282-
if item == "salad" {
283-
3.50
284-
} else if item == "muffin" {
285-
2.25
286-
} else {
287-
2.00
288-
};
278+
let price = if item == "salad" {
279+
3.50
280+
} else if item == "muffin" {
281+
2.25
282+
} else {
283+
2.00
284+
};
289285
~~~~
290286

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

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

@@ -350,7 +346,8 @@ if x {
350346
let y = if x { foo() } else { bar() };
351347
~~~
352348

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

355352
## Types
356353

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

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

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

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

401397
~~~~
402398
type MonsterSize = uint;
@@ -405,25 +401,9 @@ type MonsterSize = uint;
405401
This will provide a synonym, `MonsterSize`, for unsigned integers. It will not
406402
actually create a new, incompatible type—`MonsterSize` and `uint` can be used
407403
interchangeably, and using one where the other is expected is not a type
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-
~~~~
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.
427407

428408
## Literals
429409

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

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

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

487467
The main difference with C is that `++` and `--` are missing, and that
488468
the logical bitwise operators have higher precedence — in C, `x & 2 > 0`
489-
means `x & (2 > 0)`, but in Rust, it means `(x & 2) > 0`, which is
490-
more likely what a novice expects.
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).
491471

492472
## Syntax extensions
493473

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

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

582562
~~~
583563
# let my_number = 1;
584564
match my_number {
585-
0 => { io::println("zero") }
586-
_ => { io::println("something else") }
565+
0 => {
566+
io::println("zero")
567+
}
568+
_ => {
569+
io::println("something else")
570+
}
587571
}
588572
~~~
589573

0 commit comments

Comments
 (0)