Skip to content

Commit bd2df74

Browse files
committed
---
yaml --- r: 32759 b: refs/heads/dist-snap c: 0cabd6f h: refs/heads/master i: 32757: 3ad3e20 32755: f26830a 32751: c3148c8 v: v3
1 parent befd70d commit bd2df74

File tree

2 files changed

+167
-122
lines changed

2 files changed

+167
-122
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
10-
refs/heads/dist-snap: c2bc5122cd2122edcc0a4fd23288b19378ad2d3a
10+
refs/heads/dist-snap: 0cabd6fa1a87f5a6c522cddcbaeb2a189000dfe7
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/dist-snap/doc/tutorial.md

Lines changed: 166 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -61,41 +61,41 @@ they don't contain references to things that aren't actually defined.
6161
6262
# Getting started
6363

64-
## Installation
64+
The Rust compiler currently must be built from a [tarball], unless you
65+
are on Windows, in which case using the [installer][win-exe] is
66+
recommended.
6567

66-
The Rust compiler currently must be built from a [tarball][]. We hope
67-
to be distributing binary packages for various operating systems in
68-
the future.
68+
Since the Rust compiler is written in Rust, it must be built by
69+
a precompiled "snapshot" version of itself (made in an earlier state
70+
of development). As such, source builds require a connection to
71+
the Internet, to fetch snapshots, and an OS that can execute the
72+
available snapshot binaries.
6973

70-
The Rust compiler is slightly unusual in that it is written in Rust
71-
and therefore must be built by a precompiled "snapshot" version of
72-
itself (made in an earlier state of development). As such, source
73-
builds require that:
74+
Snapshot binaries are currently built and tested on several platforms:
7475

75-
* You are connected to the internet, to fetch snapshots.
76-
* You can at least execute snapshot binaries of one of the forms we
77-
offer them in. Currently we build and test snapshots on:
78-
* Windows (7, server 2008 r2) x86 only
79-
* Linux (various distributions) x86 and x86-64
80-
* OSX 10.6 ("Snow Leopard") or 10.7 ("Lion") x86 and x86-64
76+
* Windows (7, Server 2008 R2), x86 only
77+
* Linux (various distributions), x86 and x86-64
78+
* OSX 10.6 ("Snow Leopard") or 10.7 ("Lion"), x86 and x86-64
8179

82-
You may find other platforms work, but these are our "tier 1" supported
83-
build environments that are most likely to work. Further platforms will
84-
be added to the list in the future via cross-compilation.
80+
You may find that other platforms work, but these are our "tier 1"
81+
supported build environments that are most likely to work.
82+
83+
> ***Note:*** Windows users should read the detailed
84+
> [getting started][wiki-start] notes on the wiki. Even when using
85+
> the binary installer the windows build requires a MinGW installation,
86+
> the precise details of which are not discussed in this tutorial.
8587
8688
To build from source you will also need the following prerequisite
8789
packages:
8890

89-
* g++ 4.4 or clang++ 3.x
90-
* python 2.6 or later
91-
* perl 5.0 or later
92-
* gnu make 3.81 or later
93-
* curl
91+
* g++ 4.4 or clang++ 3.x
92+
* python 2.6 or later (but not 3.x)
93+
* perl 5.0 or later
94+
* gnu make 3.81 or later
95+
* curl
9496

9597
Assuming you're on a relatively modern *nix system and have met the
96-
prerequisites, something along these lines should work. Building from
97-
source on Windows requires some extra steps: please see the [getting
98-
started][wiki-get-started] page on the Rust wiki.
98+
prerequisites, something along these lines should work.
9999

100100
~~~~ {.notrust}
101101
$ wget http://dl.rust-lang.org/dist/rust-0.4.tar.gz
@@ -118,8 +118,9 @@ When complete, `make install` will place the following programs into
118118
* `rustdoc`, the API-documentation tool
119119
* `cargo`, the Rust package manager
120120

121-
[wiki-get-started]: https://github.com/mozilla/rust/wiki/Note-getting-started-developing-Rust
121+
[wiki-start]: https://github.com/mozilla/rust/wiki/Note-getting-started-developing-Rust
122122
[tarball]: http://dl.rust-lang.org/dist/rust-0.4.tar.gz
123+
[win-exe]: http://dl.rust-lang.org/dist/rust-0.4-install.exe
123124

124125
## Compiling your first program
125126

@@ -128,91 +129,137 @@ we have a file `hello.rs` containing this program:
128129

129130
~~~~
130131
fn main() {
131-
io::println("hello world!");
132+
io::println("hello? yes, this is rust");
132133
}
133134
~~~~
134135

135136
If the Rust compiler was installed successfully, running `rustc
136-
hello.rs` will produce a binary called `hello` (or `hello.exe`).
137+
hello.rs` will produce an executable called `hello` (or `hello.exe` on
138+
Windows) which, upon running, will likely do exactly what you expect
139+
(unless you are on Windows, in which case what it does is subject
140+
to local weather conditions).
141+
142+
> ***Note:*** That may or may not be hyperbole, but there are some
143+
> 'gotchas' to be aware of on Windows. First, the MinGW environment
144+
> must be set up perfectly. Please read [the
145+
> wiki][wiki-started]. Second, `rustc` may need to be [referred to as
146+
> `rustc.exe`][bug-3319]. It's a bummer, I know, and I am so very
147+
> sorry.
137148
138-
If you modify the program to make it invalid (for example, by changing
139-
`io::println` to some nonexistent function), and then compile it,
140-
you'll see an error message like this:
149+
[bug-3319]: https://github.com/mozilla/rust/issues/3319
150+
[wiki-started]: https://github.com/mozilla/rust/wiki/Note-getting-started-developing-Rust
151+
152+
The Rust compiler tries to provide useful information when it runs
153+
into an error. If you modify the program to make it invalid (for
154+
example, by changing `io::println` to some nonexistent function), and
155+
then compile it, you'll see an error message like this:
141156

142157
~~~~ {.notrust}
143158
hello.rs:2:4: 2:16 error: unresolved name: io::print_it
144-
hello.rs:2 io::print_it("hello world!");
159+
hello.rs:2 io::print_it("hello? yes, this is rust");
145160
^~~~~~~~~~~~
146161
~~~~
147162

148-
The Rust compiler tries to provide useful information when it runs
149-
into an error.
150-
151-
## Anatomy of a Rust program
152-
153-
In its simplest form, a Rust program is a `.rs` file with some
154-
types and functions defined in it. If it has a `main` function, it can
155-
be compiled to an executable. Rust does not allow code that's not a
163+
In its simplest form, a Rust program is a `.rs` file with some types
164+
and functions defined in it. If it has a `main` function, it can be
165+
compiled to an executable. Rust does not allow code that's not a
156166
declaration to appear at the top level of the file—all statements must
157-
live inside a function.
158-
159-
Rust programs can also be compiled as libraries, and included in other
160-
programs. The `extern mod std` directive that appears at the top of a lot of
161-
examples imports the [standard library][std]. This is described in more
162-
detail [later on](#modules-and-crates).
167+
live inside a function. Rust programs can also be compiled as
168+
libraries, and included in other programs. The `extern mod std`
169+
directive that appears at the top of many examples imports the
170+
[standard library][std], described in more detail [later
171+
on](#modules-and-crates).
163172

164173
[std]: http://doc.rust-lang.org/doc/std
165174

166175
## Editing Rust code
167176

168-
There are Vim highlighting and indentation scripts in the Rust source
169-
distribution under `src/etc/vim/`, and an emacs mode under
170-
`src/etc/emacs/`. There is a package for Sublime Text 2 at
171-
[github.com/dbp/sublime-rust](http://github.com/dbp/sublime-rust), also
172-
available through [package control](http://wbond.net/sublime_packages/package_control).
177+
There are vim highlighting and indentation scripts in the Rust source
178+
distribution under `src/etc/vim/`. There is an emacs mode under
179+
`src/etc/emacs/` called `rust-mode`, but do read the instructions
180+
included in that directory. In particular, if you are running emacs
181+
24, then using emacs's internal package manager to install `rust-mode`
182+
is the easiest way to keep it up to date. There is also a package for
183+
Sublime Text 2, available both [standalone][sublime] and through
184+
[Sublime Package Control][sublime-pkg].
173185

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

177-
# Syntax Basics
189+
[sublime]: http://github.com/dbp/sublime-rust
190+
[sublime-pkg]: http://wbond.net/sublime_packages/package_control
178191

179-
## Braces
192+
# Syntax Basics
180193

181194
Assuming you've programmed in any C-family language (C++, Java,
182-
JavaScript, C#, or PHP), Rust will feel familiar. The main surface
183-
difference to be aware of is that the bodies of `if` statements and of
184-
`while` loops *have* to be wrapped in brackets. Single-statement,
185-
bracket-less bodies are not allowed.
195+
JavaScript, C#, or PHP), Rust will feel familiar. Code is arranged
196+
in blocks delineated by curly braces; there are control structures
197+
for branching and looping, like the familiar `if` and `when`; function
198+
calls are written `myfunc(arg1, arg2)`; operators are written the same
199+
and mostly have the same precedence as in C; comments are again like C.
186200

187-
Accounting for these differences, the surface syntax of Rust
188-
statements and expressions is C-like. Function calls are written
189-
`myfunc(arg1, arg2)`, operators have mostly the same name and
190-
precedence that they have in C, comments look the same, and constructs
191-
like `if` and `while` are available:
201+
The main surface difference to be aware of is that the condition at
202+
the head of control structures like `if` and `while` do not require
203+
paretheses, while their bodies *must* be wrapped in
204+
brackets. Single-statement, bracket-less bodies are not allowed.
192205

193206
~~~~
194-
# fn it_works() {}
195-
# fn abort() {}
207+
# fn calibrate_universe() -> bool { true }
208+
# fn party_on() {}
209+
# fn panic() {}
196210
fn main() {
197-
while true {
198-
/* Ensure that basic math works. */
211+
while calibrate_universe() {
212+
/* Ensure that basic math still operates is expected */
199213
if 2*20 > 30 {
200-
// Everything is OK.
201-
it_works();
214+
party_on(); // That's a relief
202215
} else {
203-
abort();
216+
panic();
204217
}
205-
break;
206218
}
207219
}
208220
~~~~
209221

222+
The `let` keyword, introduces a local variable. By default, variables
223+
are immutable. `let mut` can be used to introduce a local variable
224+
that can be reassigned.
225+
226+
~~~~
227+
let hi = "hi";
228+
let mut count = 0;
229+
230+
while count < 10 {
231+
io::println(hi);
232+
count += 1;
233+
}
234+
~~~~
235+
236+
Although Rust can almost always infer the types of local variables, it
237+
can help readability to specify a variable's type by following it with
238+
a colon, then the type name. Local variables may shadow earlier
239+
declarations, making the earlier variables inaccessible.
240+
241+
~~~~
242+
let my_favorite_value: float = 57.8;
243+
let my_favorite_value: int = my_favorite_value as int;
244+
~~~~
245+
246+
Rust identifiers follow the same rules as C; they start with an alphabetic
247+
character or an underscore, and after that may contain any sequence of
248+
alphabetic characters, numbers, or underscores. The preferred style is to
249+
begin function, variable, and module names with a lowercase letter, using
250+
underscores where they help readability, while writing types in camel case.
251+
252+
~~~
253+
let my_variable = 100;
254+
type MyType = int; // built-in types though are _not_ camel case
255+
~~~
256+
210257
## Expression syntax
211258

212259
Though it isn't apparent in all code, there is a fundamental
213260
difference between Rust's syntax and its predecessors in this family
214261
of languages. Many constructs that are statements in C are expressions
215-
in Rust. This allows Rust to be more expressive. For example, you might
262+
in Rust, allowing code to be more concise. For example, you might
216263
write a piece of code like this:
217264

218265
~~~~
@@ -231,81 +278,79 @@ But, in Rust, you don't have to repeat the name `price`:
231278

232279
~~~~
233280
# let item = "salad";
234-
let price = if item == "salad" { 3.50 }
235-
else if item == "muffin" { 2.25 }
236-
else { 2.00 };
281+
let price = if item == "salad" {
282+
3.50
283+
} else if item == "muffin" {
284+
2.25
285+
} else {
286+
2.00
287+
};
237288
~~~~
238289

239-
Both pieces of code are exactly equivalent—they assign a value to `price`
240-
depending on the condition that holds. Note that the semicolons are omitted
241-
from the second snippet. This is important; the lack of a semicolon after the
242-
last statement in a braced block gives the whole block the value of that last
243-
expression.
290+
Both pieces of code are exactly equivalent—they assign a value to
291+
`price` depending on the condition that holds. Note that the
292+
semicolons are omitted from the blocks in the second snippet. This is
293+
important; the lack of a semicolon after the last statement in a
294+
braced block gives the whole block the value of that last expression.
244295

245296
Put another way, the semicolon in Rust *ignores the value of an expression*.
246297
Thus, if the branches of the `if` had looked like `{ 4; }`, the above example
247298
would simply assign nil (void) to `price`. But without the semicolon, each
248299
branch has a different value, and `price` gets the value of the branch that
249300
was taken.
250301

251-
This feature also works for function bodies. This function returns a boolean:
302+
In short, everything that's not a declaration (`let` for variables,
303+
`fn` for functions, et cetera) is an expression, including function bodies.
252304

253305
~~~~
254-
fn is_four(x: int) -> bool { x == 4 }
306+
fn is_four(x: int) -> bool {
307+
// No need for a return statement. The result of the expression
308+
// is used as the return value.
309+
x == 4
310+
}
255311
~~~~
256312

257-
In short, everything that's not a declaration (`let` for variables,
258-
`fn` for functions, et cetera) is an expression.
259-
260313
If all those things are expressions, you might conclude that you have
261314
to add a terminating semicolon after *every* statement, even ones that
262315
are not traditionally terminated with a semicolon in C (like `while`).
263316
That is not the case, though. Expressions that end in a block only
264317
need a semicolon if that block contains a trailing expression. `while`
265318
loops do not allow trailing expressions, and `if` statements tend to
266319
only have a trailing expression when you want to use their value for
267-
something—in which case you'll have embedded it in a bigger statement,
268-
like the `let x = ...` example above.
269-
270-
## Identifiers
271-
272-
Rust identifiers follow the same rules as C; they start with an alphabetic
273-
character or an underscore, and after that may contain any sequence of
274-
alphabetic characters, numbers, or underscores. The preferred style is to
275-
begin function, variable, and module names with a lowercase letter, using
276-
underscores where they help readability, while beginning types with a capital
277-
letter.
320+
something—in which case you'll have embedded it in a bigger statement.
278321

279-
The double-colon (`::`) is used as a module separator, so
280-
`io::println` means 'the thing named `println` in the module
281-
named `io`.
282-
283-
## Variable declaration
322+
~~~
323+
# fn foo() -> bool { true }
324+
# fn bar() -> bool { true }
325+
# fn baz() -> bool { true }
284326
285-
The `let` keyword, as we've seen, introduces a local variable. Local
286-
variables are immutable by default: `let mut` can be used to introduce
287-
a local variable that can be reassigned. Global constants can be
288-
defined with `const`:
327+
// `let` is not an expression, so it is semi-colon terminated;
328+
let x = foo();
289329
290-
~~~~
291-
const REPEAT: int = 5;
292-
fn main() {
293-
let hi = "Hi!";
294-
let mut count = 0;
295-
while count < REPEAT {
296-
io::println(hi);
297-
count += 1;
298-
}
299-
}
300-
~~~~
330+
// When used in statement position, bracy expressions do not
331+
// usually need to be semicolon terminated
332+
if x {
333+
bar();
334+
} else {
335+
baz();
336+
} // No semi-colon
337+
338+
// Although, if `bar` and `baz` have non-nil return types, and
339+
// we try to use them as the tail expressions, rustc will
340+
// make us terminate the expression.
341+
if x {
342+
bar()
343+
} else {
344+
baz()
345+
}; // Semi-colon to ignore non-nil block type
301346
302-
Local variables may shadow earlier declarations, making the earlier variables
303-
inaccessible.
347+
// An `if` embedded in `let` again requires a semicolon to terminate
348+
// the `let` statement
349+
let y = if x { foo() } else { bar() };
350+
~~~
304351

305-
~~~~
306-
let my_favorite_value: float = 57.8;
307-
let my_favorite_value: int = my_favorite_value as int;
308-
~~~~
352+
This may sound a bit intricate, but it is super-useful, and it will
353+
grow on you (hopefully).
309354

310355
## Types
311356

0 commit comments

Comments
 (0)