tutorial.md 106.3 KB
Newer Older
B
Brian Anderson 已提交
1
% The Rust Language Tutorial
2

3 4 5 6 7 8 9
<div style="border: 2px solid red; padding:5px;">
The tutorial is undergoing a complete re-write as <a href="guide.html">the Guide</a>.
Until it's ready, this tutorial is the right place to come to start learning
Rust.  Please submit improvements as pull requests, but understand that
eventually it will be going away.
</div>

10 11
# Introduction

12 13
Rust is a programming language with a focus on type safety, memory
safety, concurrency and performance. It is intended for writing
14
large-scale, high-performance software that is free from several
15 16 17 18 19
classes of common errors. Rust has a sophisticated memory model that
encourages efficient data structures and safe concurrency patterns,
forbidding invalid memory accesses that would otherwise cause
segmentation faults. It is statically typed and compiled ahead of
time.
20

21
As a multi-paradigm language, Rust supports writing code in
22 23
procedural, functional and object-oriented styles. Some of its
pleasant high-level features include:
24

B
Brian Anderson 已提交
25 26 27
* **Type inference.** Type annotations on local variable declarations
  are optional.
* **Safe task-based concurrency.** Rust's lightweight tasks do not share
28
  memory, instead communicating through messages.
B
Brian Anderson 已提交
29 30 31
* **Higher-order functions.** Efficient and flexible closures provide
  iteration and other control structures
* **Pattern matching and algebraic data types.** Pattern matching on
32 33 34 35
  Rust's enumeration types (a more powerful version of C's enums,
  similar to algebraic data types in functional languages) is a
  compact and expressive way to encode program logic.
* **Polymorphism.** Rust has type-parametric functions and
B
Brian Anderson 已提交
36
  types, type classes and OO-style interfaces.
37

38
## Scope
39

40 41
This is an introductory tutorial for the Rust programming language. It
covers the fundamentals of the language, including the syntax, the
42
type system and memory model, generics, and modules. [Additional
43
tutorials](#what-next?) cover specific language features in greater
44
depth.
45

46
This tutorial assumes that the reader is already familiar with one or
B
Brian Anderson 已提交
47 48
more languages in the C family. Understanding of pointers and general
memory management techniques will help.
49

50 51
## Conventions

52 53
Throughout the tutorial, language keywords and identifiers defined in
example code are displayed in `code font`.
54

55
Code snippets are indented, and also shown in a monospaced font. Not
56 57 58
all snippets constitute whole programs. For brevity, we'll often show
fragments of programs that don't compile on their own. To try them
out, you might have to wrap them in `fn main() { ... }`, and make sure
59
they don't contain references to names that aren't actually defined.
60

61
> *Warning:* Rust is a language under ongoing development. Notes
62 63 64
> about potential changes to the language, implementation
> deficiencies, and other caveats appear offset in blockquotes.

65 66
# Getting started

67 68 69 70
There are two ways to install the Rust compiler: by building from source or
by downloading prebuilt binaries or installers for your platform. The
[install page][rust-install] contains links to download binaries for both
the nightly build and the most current Rust major release. For Windows and
71
OS X, the install page provides links to native installers.
72

73 74 75 76 77 78
> *Note:* Windows users should read the detailed
> [Getting started][wiki-start] notes on the wiki. Even when using
> the binary installer, the Windows build requires a MinGW installation,
> the precise details of which are not discussed here.

For Linux and OS X, the install page provides links to binary tarballs.
A
Adrien Brault 已提交
79
To install the Rust compiler from a binary tarball, download
80
the binary package, extract it, and execute the `install.sh` script in
81 82 83 84
the root directory of the package.

To build the Rust compiler from source, you will need to obtain the source through
[Git][git] or by downloading the source package from the [install page][rust-install].
85

86 87
Since the Rust compiler is written in Rust, it must be built by
a precompiled "snapshot" version of itself (made in an earlier state
88 89
of development). The source build automatically fetches these snapshots
from the Internet on our supported platforms.
90

91
Snapshot binaries are currently built and tested on several platforms:
92

D
Daniel Micay 已提交
93
* Windows (7, 8, Server 2008 R2), x86 only
94
* Linux (2.6.18 or later, various distributions), x86 and x86-64
95
* OSX 10.7 (Lion) or greater, x86 and x86-64
96

97 98 99
You may find that other platforms work, but these are our "tier 1"
supported build environments that are most likely to work.

B
Brian Anderson 已提交
100 101
[wiki-start]: https://github.com/rust-lang/rust/wiki/Note-getting-started-developing-Rust
[git]: https://github.com/rust-lang/rust.git
102
[rust-install]: http://www.rust-lang.org/install.html
103 104 105 106

To build from source you will also need the following prerequisite
packages:

107
* g++ 4.7 or clang++ 3.x
108 109 110 111
* python 2.6 or later (but not 3.x)
* perl 5.0 or later
* gnu make 3.81 or later
* curl
112

113 114
If you've fulfilled those prerequisites, something along these lines
should work.
115

F
Florian Gilcher 已提交
116
~~~~console
117
$ curl -O https://static.rust-lang.org/dist/rust-nightly.tar.gz
B
Brian Anderson 已提交
118 119
$ tar -xzf rust-nightly.tar.gz
$ cd rust-nightly
120 121 122 123
$ ./configure
$ make && make install
~~~~

124
You may need to use `sudo make install` if you do not normally have
B
Brian Anderson 已提交
125 126
permission to modify the destination directory. The install locations
can be adjusted by passing a `--prefix` argument to
127
`configure`. Various other options are also supported: pass `--help`
B
Brian Anderson 已提交
128
for more information on them.
129

130
When complete, `make install` will place several programs into
C
Corey Richardson 已提交
131 132
`/usr/local/bin`: `rustc`, the Rust compiler, and `rustdoc`, the
API-documentation tool.
133

134 135
[tarball]: https://static.rust-lang.org/dist/rust-nightly.tar.gz
[win-exe]: https://static.rust-lang.org/dist/rust-nightly-install.exe
136 137 138 139 140 141 142

## Compiling your first program

Rust program files are, by convention, given the extension `.rs`. Say
we have a file `hello.rs` containing this program:

~~~~
P
Patrick Walton 已提交
143
fn main() {
144
    println!("hello?");
145 146
}
~~~~
147
> *Note:* An identifier followed by an exclamation point, like
148 149 150
> `println!`, is a macro invocation.  Macros are explained
> [later](#syntax-extensions); for now just remember to include the
> exclamation point.
151 152

If the Rust compiler was installed successfully, running `rustc
153
hello.rs` will produce an executable called `hello` (or `hello.exe` on
B
Brian Anderson 已提交
154
Windows) which, upon running, will likely do exactly what you expect.
155

156 157
The Rust compiler tries to provide useful information when it encounters an
error. If you introduce an error into the program (for example, by changing
158
`println!` to some nonexistent macro), and then compile it, you'll see
159
an error message like this:
160

F
Florian Gilcher 已提交
161
~~~~text
162 163
hello.rs:2:5: 2:24 error: macro undefined: 'print_with_unicorns'
hello.rs:2     print_with_unicorns!("hello?");
C
Corey Richardson 已提交
164
               ^~~~~~~~~~~~~~~~~~~
165 166
~~~~

167 168 169
In its simplest form, a Rust program is a `.rs` file with some types
and functions defined in it. If it has a `main` function, it can be
compiled to an executable. Rust does not allow code that's not a
170
declaration to appear at the top level of the file: all statements must
171
live inside a function.  Rust programs can also be compiled as
C
Corey Richardson 已提交
172
libraries, and included in other programs, even ones not written in Rust.
173 174 175

## Editing Rust code

176 177 178 179 180 181 182
There are vim highlighting and indentation scripts in the Rust source
distribution under `src/etc/vim/`. There is an emacs mode under
`src/etc/emacs/` called `rust-mode`, but do read the instructions
included in that directory. In particular, if you are running emacs
24, then using emacs's internal package manager to install `rust-mode`
is the easiest way to keep it up to date. There is also a package for
Sublime Text 2, available both [standalone][sublime] and through
183 184
[Sublime Package Control][sublime-pkg], and support for Kate
under `src/etc/kate`.
185

186 187 188
A community-maintained list of available Rust tooling is [on the
wiki][wiki-packages].

189
There is ctags support via `src/etc/ctags.rust`, but many other
190
tools and editors are not yet supported. If you end up writing a Rust
191 192
mode for your favorite editor, let us know so that we can link to it.

193
[sublime]: http://github.com/jhasse/sublime-rust
194
[sublime-pkg]: http://wbond.net/sublime_packages/package_control
195

196
# Syntax basics
197 198

Assuming you've programmed in any C-family language (C++, Java,
199 200
JavaScript, C#, or PHP), Rust will feel familiar. Code is arranged
in blocks delineated by curly braces; there are control structures
201
for branching and looping, like the familiar `if` and `while`; function
202
calls are written `myfunc(arg1, arg2)`; operators are written the same
203
and mostly have the same precedence as in C; comments are again like C;
204
module names are separated with double-colon (`::`) as with C++.
205

206
The main surface difference to be aware of is that the condition at
207
the head of control structures like `if` and `while` does not require
T
Tim Chevalier 已提交
208 209
parentheses, while their bodies *must* be wrapped in
braces. Single-statement, unbraced bodies are not allowed.
210 211

~~~~
B
Brian Anderson 已提交
212
# mod universe { pub fn recalibrate() -> bool { true } }
213
fn main() {
214 215 216
    /* A simple loop */
    loop {
        // A tricky calculation
217
        if universe::recalibrate() {
218
            return;
P
Patrick Walton 已提交
219
        }
220 221 222 223
    }
}
~~~~

T
Tim Chevalier 已提交
224 225 226
The `let` keyword introduces a local variable. Variables are immutable by
default. To introduce a local variable that you can re-assign later, use `let
mut` instead.
227 228 229

~~~~
let hi = "hi";
230
let mut count = 0i;
231 232

while count < 10 {
233
    println!("count is {}", count);
234 235 236 237
    count += 1;
}
~~~~

238 239 240 241
Although Rust can almost always infer the types of local variables, you can
specify a variable's type by following it in the `let` with a colon, then the
type name. Static items, on the other hand, always require a type annotation.

242 243

~~~~
D
Daniel Micay 已提交
244
static MONSTER_FACTOR: f64 = 57.8;
245
let monster_size = MONSTER_FACTOR * 10.0;
246
let monster_size: int = 50;
247 248
~~~~

T
Tim Chevalier 已提交
249
Local variables may shadow earlier declarations, as in the previous example:
D
Daniel Micay 已提交
250
`monster_size` was first declared as a `f64`, and then a second
251
`monster_size` was declared as an `int`. If you were to actually compile this
252
example, though, the compiler would determine that the first `monster_size` is
T
Tim Chevalier 已提交
253 254
unused and issue a warning (because this situation is likely to indicate a
programmer error). For occasions where unused variables are intentional, their
255
names may be prefixed with an underscore to silence the warning, like `let
T
Tim Chevalier 已提交
256
_monster_size = 50;`.
B
Brian Anderson 已提交
257

258
Rust identifiers start with an alphabetic
259 260
character or an underscore, and after that may contain any sequence of
alphabetic characters, numbers, or underscores. The preferred style is to
B
Brian Anderson 已提交
261
write function, variable, and module names with lowercase letters, using
262 263 264
underscores where they help readability, while writing types in camel case.

~~~
265
let my_variable = 100i;
B
Brian Anderson 已提交
266
type MyType = int;     // primitive types are _not_ camel case
267 268
~~~

269
## Expressions and semicolons
270 271

Though it isn't apparent in all code, there is a fundamental
272 273
difference between Rust's syntax and predecessors like C.
Many constructs that are statements in C are expressions
274
in Rust, allowing code to be more concise. For example, you might
P
Patrick Walton 已提交
275 276 277 278
write a piece of code like this:

~~~~
# let item = "salad";
279
let price: f64;
P
Patrick Walton 已提交
280 281 282 283 284 285 286
if item == "salad" {
    price = 3.50;
} else if item == "muffin" {
    price = 2.25;
} else {
    price = 2.00;
}
287 288
~~~~

P
Patrick Walton 已提交
289
But, in Rust, you don't have to repeat the name `price`:
290 291

~~~~
P
Patrick Walton 已提交
292
# let item = "salad";
293
let price: f64 =
294 295 296 297 298 299 300
    if item == "salad" {
        3.50
    } else if item == "muffin" {
        2.25
    } else {
        2.00
    };
301 302
~~~~

T
Tim Chevalier 已提交
303
Both pieces of code are exactly equivalent: they assign a value to
304
`price` depending on the condition that holds. Note that there
T
Tim Chevalier 已提交
305 306
are no semicolons in the blocks of the second snippet. This is
important: the lack of a semicolon after the last statement in a
307
braced block gives the whole block the value of that last expression.
P
Patrick Walton 已提交
308

309 310
Put another way, the semicolon in Rust *ignores the value of an expression*.
Thus, if the branches of the `if` had looked like `{ 4; }`, the above example
311
would simply assign `()` (unit or void) to `price`. But without the semicolon, each
312 313
branch has a different value, and `price` gets the value of the branch that
was taken.
314

T
Tim Chevalier 已提交
315
In short, everything that's not a declaration (declarations are `let` for
316
variables; `fn` for functions; and any top-level named items such as
G
gifnksm 已提交
317
[traits](#traits), [enum types](#enums), and static items) is an
T
Tim Chevalier 已提交
318
expression, including function bodies.
319 320

~~~~
321 322 323 324 325
fn is_four(x: int) -> bool {
   // No need for a return statement. The result of the expression
   // is used as the return value.
   x == 4
}
326 327
~~~~

328
## Primitive types and literals
329

330 331
There are general signed and unsigned integer types, `int` and `uint`,
as well as 8-, 16-, 32-, and 64-bit variants, `i8`, `u16`, etc.
M
Matt Carberry 已提交
332
Integers can be written in decimal (`144`), hexadecimal (`0x90`), octal (`0o70`), or
333 334
binary (`0b10010000`) base. Each integral type has a corresponding literal
suffix that can be used to indicate the type of a literal: `i` for `int`,
335
`u` for `uint`, `i8` for the `i8` type.
336

T
Tim Chevalier 已提交
337
In the absence of an integer literal suffix, Rust will infer the
338 339
integer type based on type annotations and function signatures in the
surrounding program. In the absence of any type information at all,
340
Rust will report an error and request that the type be specified explicitly.
341 342

~~~~
343
let a: int = 1;  // `a` is an `int`
344 345 346
let b = 10i;     // `b` is an `int`, due to the `i` suffix
let c = 100u;    // `c` is a `uint`
let d = 1000i32; // `d` is an `i32`
347 348
~~~~

D
Daniel Micay 已提交
349
There are two floating-point types: `f32`, and `f64`.
350 351
Floating-point numbers are written `0.0`, `1e6`, or `2.1e-4`.
Like integers, floating-point literals are inferred to the correct type.
C
Corey Richardson 已提交
352
Suffixes `f32`, and `f64` can be used to create literals of a specific type.
353

354
The keywords `true` and `false` produce literals of type `bool`.
355

356
Characters, the `char` type, are four-byte Unicode codepoints,
357 358
whose literals are written between single quotes, as in `'x'`.
Just like C, Rust understands a number of character escapes, using the backslash
359
character, such as `\n`, `\r`, and `\t`. String literals,
360 361
written between double quotes, allow the same escape sequences, and do no
other processing, unlike languages such as PHP or shell.
362 363 364 365 366 367

On the other hand, raw string literals do not process any escape sequences.
They are written as `r##"blah"##`, with a matching number of zero or more `#`
before the opening and after the closing quote, and can contain any sequence of
characters except their closing delimiter.  More on strings
[later](#vectors-and-strings).
368

369
The unit type, written `()`, has a single value, also written `()`.
370 371 372

## Operators

373
Rust's set of operators contains very few surprises. Arithmetic is done with
374
`*`, `/`, `%`, `+`, and `-` (multiply, quotient, remainder, add, and subtract). `-` is
375
also a unary prefix operator that negates numbers. As in C, the bitwise operators
376
`>>`, `<<`, `&`, `|`, and `^` are also supported.
377

378 379
Note that, if applied to an integer value, `!` flips all the bits (bitwise
NOT, like `~` in C).
380 381 382 383 384

The comparison operators are the traditional `==`, `!=`, `<`, `>`,
`<=`, and `>=`. Short-circuiting (lazy) boolean operators are written
`&&` (and) and `||` (or).

385 386 387 388 389 390
For compile-time type casting, Rust uses the binary `as` operator.  It takes
an expression on the left side and a type on the right side and will, if a
meaningful conversion exists, convert the result of the expression to the
given type. Generally, `as` is only used with the primitive numeric types or
pointers, and is not overloadable.  [`transmute`][transmute] can be used for
unsafe C-like casting of same-sized types.
391 392

~~~~
D
Daniel Micay 已提交
393
let x: f64 = 4.0;
394
let y: uint = x as uint;
P
Patrick Walton 已提交
395
assert!(y == 4u);
396 397
~~~~

398
[transmute]: http://doc.rust-lang.org/std/mem/fn.transmute.html
399

400 401
## Syntax extensions

402 403
*Syntax extensions* are special forms that are not built into the language,
but are instead provided by the libraries. To make it clear to the reader when
T
Tim Chevalier 已提交
404 405
a name refers to a syntax extension, the names of all syntax extensions end
with `!`. The standard library defines a few syntax extensions, the most
A
Alex Crichton 已提交
406 407 408
useful of which is [`format!`][fmt], a `sprintf`-like text formatter that you
will often see in examples, and its related family of macros: `print!`,
`println!`, and `write!`.
409

C
Corey Richardson 已提交
410
`format!` draws syntax from Python, but contains many of the same principles
A
Alex Crichton 已提交
411 412
that [printf][pf] has. Unlike printf, `format!` will give you a compile-time
error when the types of the directives don't match the types of the arguments.
413

414
~~~
415
// `{}` will print the "default format" of a type
416
println!("{} is {}", "the answer", 43i);
417
~~~
418

419 420 421 422 423 424 425
~~~~
extern crate debug;

# fn main() {
# let mystery_object = ();
// `{:?}` will conveniently print any type,
// but requires the `debug` crate to be linked in
A
Alex Crichton 已提交
426
println!("what is this thing: {:?}", mystery_object);
427
# }
428 429
~~~~

430
[pf]: http://en.cppreference.com/w/cpp/io/c/fprintf
431
[fmt]: http://doc.rust-lang.org/std/fmt/
432

433 434 435
You can define your own syntax extensions with the macro system. For details,
see the [macro tutorial][macros]. Note that macro definition is currently
considered an unstable feature.
T
Tim Chevalier 已提交
436

437 438 439 440
# Control structures

## Conditionals

T
Tim Chevalier 已提交
441 442
We've seen `if` expressions a few times already. To recap, braces are
compulsory, an `if` can have an optional `else` clause, and multiple
443 444 445 446
`if`/`else` constructs can be chained together:

~~~~
if false {
447
    println!("that's odd");
448
} else if true {
449
    println!("right");
450
} else {
451
    println!("neither true nor false");
452 453 454
}
~~~~

T
Tim Chevalier 已提交
455 456 457 458
The condition given to an `if` construct *must* be of type `bool` (no
implicit conversion happens). If the arms are blocks that have a
value, this value must be of the same type for every arm in which
control reaches the end of the block:
459 460 461 462 463

~~~~
fn signum(x: int) -> int {
    if x < 0 { -1 }
    else if x > 0 { 1 }
C
Corey Richardson 已提交
464
    else { 0 }
465 466 467 468 469
}
~~~~

## Pattern matching

470
Rust's `match` construct is a generalized, cleaned-up version of C's
T
Tim Chevalier 已提交
471
`switch` construct. You provide it with a value and a number of
472
*arms*, each labeled with a pattern, and the code compares the value
T
Tim Chevalier 已提交
473 474
against each pattern in order until one matches. The matching pattern
executes its corresponding arm.
475 476

~~~~
477
let my_number = 1i;
478
match my_number {
479 480 481 482
  0     => println!("zero"),
  1 | 2 => println!("one or two"),
  3..10 => println!("three to ten"),
  _     => println!("something else")
483 484 485
}
~~~~

486
Unlike in C, there is no "falling through" between arms: only one arm
T
Tim Chevalier 已提交
487
executes, and it doesn't have to explicitly `break` out of the
488 489
construct when it is finished.

490 491 492 493 494
A `match` arm consists of a *pattern*, then a fat arrow `=>`, followed
by an *action* (expression). Each case is separated by commas. It is
often convenient to use a block expression for each case, in which case
the commas are optional as shown below. Literals are valid patterns and
match only their own value. A single arm may match multiple different
495 496 497 498 499 500
patterns by combining them with the pipe operator (`|`), so long as
every pattern binds the same set of variables (see "destructuring"
below). Ranges of numeric literal patterns can be expressed with two
dots, as in `M..N`. The underscore (`_`) is a wildcard pattern that
matches any single value. (`..`) is a different wildcard that can match
one or more fields in an `enum` variant.
B
Brian Anderson 已提交
501 502

~~~
503
# let my_number = 1i;
504
match my_number {
505 506
  0 => { println!("zero") }
  _ => { println!("something else") }
B
Brian Anderson 已提交
507 508 509
}
~~~

T
Tim Chevalier 已提交
510 511 512 513
`match` constructs must be *exhaustive*: they must have an arm
covering every possible case. For example, the typechecker would
reject the previous example if the arm with the wildcard pattern was
omitted.
514

T
Tim Chevalier 已提交
515
A powerful application of pattern matching is *destructuring*:
516
matching in order to bind names to the contents of data types.
517

518
> *Note:* The following code makes use of tuples (`(f64, f64)`) which
519 520
> are explained in section 5.3. For now you can think of tuples as a list of
> items.
521 522

~~~~
D
Daniel Micay 已提交
523 524
use std::f64;
fn angle(vector: (f64, f64)) -> f64 {
525
    let pi = f64::consts::PI;
P
Patrick Walton 已提交
526
    match vector {
D
Daniel Micay 已提交
527
      (0.0, y) if y < 0.0 => 1.5 * pi,
528
      (0.0, _) => 0.5 * pi,
529
      (x, y) => (y / x).atan()
530 531 532 533
    }
}
~~~~

T
Tim Chevalier 已提交
534
A variable name in a pattern matches any value, *and* binds that name
D
Daniel Micay 已提交
535
to the value of the matched value inside of the arm's action. Thus, `(0.0,
536
y)` matches any tuple whose first element is zero, and binds `y` to
537
the second element. `(x, y)` matches any two-element tuple, and binds both
538 539 540
elements to variables. `(0.0,_)` matches any tuple whose first element is zero
and does not bind anything to the second element.

541 542 543 544
A subpattern can also be bound to a variable, using `variable @ pattern`. For
example:

~~~~
545
# let age = 23i;
546 547 548 549 550
match age {
    a @ 0..20 => println!("{} years old", a),
    _ => println!("older than 21")
}
~~~~
551

T
Tim Chevalier 已提交
552 553 554 555 556 557
Any `match` arm can have a guard clause (written `if EXPR`), called a
*pattern guard*, which is an expression of type `bool` that
determines, after the pattern is found to match, whether the arm is
taken or not. The variables bound by the pattern are in scope in this
guard expression. The first arm in the `angle` example shows an
example of a pattern guard.
558

559
You've already seen simple `let` bindings, but `let` is a little
T
Tim Chevalier 已提交
560 561 562
fancier than you've been led to believe. It, too, supports destructuring
patterns. For example, you can write this to extract the fields from a
tuple, introducing two variables at once: `a` and `b`.
563 564 565 566 567 568

~~~~
# fn get_tuple_of_two_ints() -> (int, int) { (1, 1) }
let (a, b) = get_tuple_of_two_ints();
~~~~

569 570 571 572 573 574 575 576
Let bindings only work with _irrefutable_ patterns: that is, patterns that can
never fail to match. This excludes `let` from matching literals and most `enum`
variants as binding patterns, since most such patterns are not irrefutable. For
example, this will not compile:

~~~~{ignore}
let (a, 2) = (1, 2);
~~~~
577 578 579

## Loops

T
Tim Chevalier 已提交
580 581
`while` denotes a loop that iterates as long as its given condition
(which must have type `bool`) evaluates to `true`. Inside a loop, the
C
Corey Richardson 已提交
582
keyword `break` aborts the loop, and `continue` aborts the current
T
Tim Chevalier 已提交
583
iteration and continues with the next.
584

585
~~~~
586
let mut cake_amount = 8i;
587 588 589 590 591
while cake_amount > 0 {
    cake_amount -= 1;
}
~~~~

T
Tim Chevalier 已提交
592
`loop` denotes an infinite loop, and is the preferred way of writing `while true`:
593

594
~~~~
595
let mut x = 5u;
B
Brian Anderson 已提交
596
loop {
597 598
    x += x - 3;
    if x % 5 == 0 { break; }
599
    println!("{}", x);
600 601 602 603 604 605
}
~~~~

This code prints out a weird sequence of numbers and stops as soon as
it finds one that can be divided by five.

606
There is also a for-loop that can be used to iterate over a range of numbers:
607 608

~~~~
609
for n in range(0u, 5) {
610 611 612 613 614 615
    println!("{}", n);
}
~~~~

The snippet above prints integer numbers under 5 starting at 0.

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
More generally, a for loop works with anything implementing the `Iterator` trait.
Data structures can provide one or more methods that return iterators over
their contents. For example, strings support iteration over their contents in
various ways:

~~~~
let s = "Hello";
for c in s.chars() {
    println!("{}", c);
}
~~~~

The snippet above prints the characters in "Hello" vertically, adding a new
line after each character.

631

632
# Data structures
633

P
Patrick Walton 已提交
634
## Structs
635

P
Patrick Walton 已提交
636 637 638
Rust struct types must be declared before they are used using the `struct`
syntax: `struct Name { field1: T1, field2: T2 [, ...] }`, where `T1`, `T2`,
... denote types. To construct a struct, use the same syntax, but leave off
639
the `struct`: for example: `Point { x: 1.0, y: 2.0 }`.
640

P
Patrick Walton 已提交
641
Structs are quite similar to C structs and are even laid out the same way in
642 643
memory (so you can read from a Rust struct in C, and vice-versa). Use the dot
operator to access struct fields, as in `mypoint.x`.
644 645

~~~~
646
struct Point {
D
Daniel Micay 已提交
647 648
    x: f64,
    y: f64
P
Patrick Walton 已提交
649
}
650 651
~~~~

652 653
Structs have "inherited mutability", which means that any field of a struct
may be mutable, if the struct is in a mutable slot.
654 655 656

With a value (say, `mypoint`) of such a type in a mutable location, you can do
`mypoint.y += 1.0`. But in an immutable location, such an assignment to a
657
struct without inherited mutability would result in a type error.
658

659
~~~~ {.ignore}
D
Daniel Micay 已提交
660
# struct Point { x: f64, y: f64 }
661 662 663
let mut mypoint = Point { x: 1.0, y: 1.0 };
let origin = Point { x: 0.0, y: 0.0 };

664
mypoint.y += 1.0; // `mypoint` is mutable, and its fields as well
665 666 667
origin.y += 1.0; // ERROR: assigning to immutable field
~~~~

668
`match` patterns destructure structs. The basic syntax is
669
`Name { fieldname: pattern, ... }`:
670

671
~~~~
D
Daniel Micay 已提交
672
# struct Point { x: f64, y: f64 }
P
Patrick Walton 已提交
673
# let mypoint = Point { x: 0.0, y: 0.0 };
674
match mypoint {
675
    Point { x: 0.0, y: yy } => println!("{}", yy),
676
    Point { x: xx,  y: yy } => println!("{} {}", xx, yy)
677 678 679
}
~~~~

P
Patrick Walton 已提交
680 681
In general, the field names of a struct do not have to appear in the same
order they appear in the type. When you are not interested in all
A
Alex Crichton 已提交
682 683
the fields of a struct, a struct pattern may end with `, ..` (as in
`Name { field1, .. }`) to indicate that you're ignoring all other fields.
684 685 686 687
Additionally, struct fields have a shorthand matching form that simply
reuses the field name as the binding name.

~~~
D
Daniel Micay 已提交
688
# struct Point { x: f64, y: f64 }
689 690
# let mypoint = Point { x: 0.0, y: 0.0 };
match mypoint {
691
    Point { x, .. } => println!("{}", x)
692 693 694
}
~~~

695 696
## Enums

697 698
Enums are datatypes with several alternate representations. A simple `enum`
defines one or more constants, all of which have the same type:
699 700

~~~~
701 702 703 704 705
enum Direction {
    North,
    East,
    South,
    West
706 707 708
}
~~~~

709 710 711 712 713
Each variant of this enum has a unique and constant integral discriminator
value. If no explicit discriminator is specified for a variant, the value
defaults to the value of the previous variant plus one. If the first variant
does not have a discriminator, it defaults to 0. For example, the value of
`North` is 0, `East` is 1, `South` is 2, and `West` is 3.
714

715 716 717 718
When an enum has simple integer discriminators, you can apply the `as` cast
operator to convert a variant to its discriminator value as an `int`:

~~~~
J
Jonas Hietala 已提交
719 720
# enum Direction { North, East, South, West }
println!( "North => {}", North as int );
721 722 723
~~~~

It is possible to set the discriminator values to chosen constant values:
724 725

~~~~
726 727 728 729
enum Color {
  Red = 0xff0000,
  Green = 0x00ff00,
  Blue = 0x0000ff
730 731 732
}
~~~~

733
Variants do not have to be simple values; they may be more complex:
734

735 736 737 738 739 740 741
~~~~
# struct Point { x: f64, y: f64 }
enum Shape {
    Circle(Point, f64),
    Rectangle(Point, Point)
}
~~~~
742

743 744 745 746 747 748 749 750
A value of this type is either a `Circle`, in which case it contains a
`Point` struct and a f64, or a `Rectangle`, in which case it contains
two `Point` structs. The run-time representation of such a value
includes an identifier of the actual form that it holds, much like the
"tagged union" pattern in C, but with better static guarantees.

This declaration defines a type `Shape` that can refer to such shapes, and two
functions, `Circle` and `Rectangle`, which can be used to construct values of
J
Jonas Hietala 已提交
751 752 753 754 755 756 757 758 759
the type.

To create a new `Circle`, write:

~~~~
# struct Point { x: f64, y: f64 }
# enum Shape { Circle(Point, f64), Rectangle(Point, Point) }
let circle = Circle(Point { x: 0.0, y: 0.0 }, 10.0);
~~~~
760 761 762 763

All of these variant constructors may be used as patterns. The only way to
access the contents of an enum instance is the destructuring of a match. For
example:
764 765

~~~~
D
Daniel Micay 已提交
766
use std::f64;
J
Jonas Hietala 已提交
767

D
Daniel Micay 已提交
768 769 770
# struct Point {x: f64, y: f64}
# enum Shape { Circle(Point, f64), Rectangle(Point, Point) }
fn area(sh: Shape) -> f64 {
771
    match sh {
772
        Circle(_, size) => f64::consts::PI * size * size,
773
        Rectangle(Point { x, y }, Point { x: x2, y: y2 }) => (x2 - x) * (y2 - y)
774 775
    }
}
J
Jonas Hietala 已提交
776 777 778

let rect = Rectangle(Point { x: 0.0, y: 0.0 }, Point { x: 2.0, y: 2.0 });
println!("area: {}", area(rect));
779 780
~~~~

781 782
Use a lone `_` to ignore an individual field. Ignore all fields of a variant
like: `Circle(..)`. Nullary enum patterns are written without parentheses:
783 784

~~~~
D
Daniel Micay 已提交
785
# struct Point { x: f64, y: f64 }
786 787
# enum Direction { North, East, South, West }
fn point_from_direction(dir: Direction) -> Point {
788
    match dir {
D
Daniel Micay 已提交
789 790 791 792
        North => Point { x:  0.0, y:  1.0 },
        East  => Point { x:  1.0, y:  0.0 },
        South => Point { x:  0.0, y: -1.0 },
        West  => Point { x: -1.0, y:  0.0 }
793 794 795 796
    }
}
~~~~

797
Enum variants may also be structs. For example:
798 799

~~~~
J
Jonas Hietala 已提交
800
#![feature(struct_variant)]
D
Daniel Micay 已提交
801
use std::f64;
J
Jonas Hietala 已提交
802

D
Daniel Micay 已提交
803 804
# struct Point { x: f64, y: f64 }
# fn square(x: f64) -> f64 { x * x }
805
enum Shape {
D
Daniel Micay 已提交
806
    Circle { center: Point, radius: f64 },
807
    Rectangle { top_left: Point, bottom_right: Point }
808
}
D
Daniel Micay 已提交
809
fn area(sh: Shape) -> f64 {
810
    match sh {
A
Alex Crichton 已提交
811
        Circle { radius: radius, .. } => f64::consts::PI * square(radius),
812
        Rectangle { top_left: top_left, bottom_right: bottom_right } => {
A
Alex Crichton 已提交
813
            (bottom_right.x - top_left.x) * (top_left.y - bottom_right.y)
814 815
        }
    }
816
}
J
Jonas Hietala 已提交
817 818 819 820 821 822 823 824

fn main() {
    let rect = Rectangle {
        top_left: Point { x: 0.0, y: 0.0 },
        bottom_right: Point { x: 2.0, y: -2.0 }
    };
    println!("area: {}", area(rect));
}
825
~~~~
826

827
> *Note:* This feature of the compiler is currently gated behind the
828 829 830
> `#[feature(struct_variant)]` directive. More about these directives can be
> found in the manual.

831 832
## Tuples

C
Corey Richardson 已提交
833 834 835
Tuples in Rust behave exactly like structs, except that their fields do not
have names. Thus, you cannot access their fields with dot notation.  Tuples
can have any arity (number of elements) except for 0 (though you may consider
836
unit, `()`, as the empty tuple if you like).
837 838

~~~~
D
Daniel Micay 已提交
839
let mytup: (int, int, f64) = (10, 20, 30.0);
840
match mytup {
841
  (a, b, c) => println!("{}", a + b + (c as int))
842 843
}
~~~~
844

T
Tim Chevalier 已提交
845 846
## Tuple structs

847 848 849 850
Rust also has _tuple structs_, which behave like both structs and tuples,
except that, unlike tuples, tuple structs have names (so `Foo(1, 2)` has a
different type from `Bar(1, 2)`), and tuple structs' _fields_ do not have
names.
T
Tim Chevalier 已提交
851 852

For example:
N
Noufal Ibrahim 已提交
853

T
Tim Chevalier 已提交
854
~~~~
D
Daniel Micay 已提交
855
struct MyTup(int, int, f64);
T
Tim Chevalier 已提交
856 857
let mytup: MyTup = MyTup(10, 20, 30.0);
match mytup {
858
  MyTup(a, b, c) => println!("{}", a + b + (c as int))
T
Tim Chevalier 已提交
859 860 861
}
~~~~

862 863 864 865 866 867 868 869 870 871 872 873
<a name="newtype"></a>

There is a special case for tuple structs with a single field, which are
sometimes called "newtypes" (after Haskell's "newtype" feature). These are
used to define new types in such a way that the new name is not just a
synonym for an existing type but is rather its own distinct type.

~~~~
struct GizmoId(int);
~~~~

Types like this can be useful to differentiate between data that have
C
Corey Richardson 已提交
874
the same underlying type but must be used in different ways.
875 876 877 878 879 880 881

~~~~
struct Inches(int);
struct Centimeters(int);
~~~~

The above definitions allow for a simple way for programs to avoid
882 883 884 885 886 887 888 889 890
confusing numbers that correspond to different units. Their integer
values can be extracted with pattern matching:

~~~
# struct Inches(int);
let length_with_unit = Inches(10);
let Inches(integer_length) = length_with_unit;
println!("length is {} inches", integer_length);
~~~
891

892
# Functions
893 894 895

We've already seen several function definitions. Like all other static
declarations, such as `type`, functions can be declared both at the
896
top level and inside other functions (or in modules, which we'll come
897
back to [later](#crates-and-the-module-system)). The `fn` keyword introduces a
898
function. A function has an argument list, which is a parenthesized
899
list of `name: type` pairs separated by commas. An arrow `->`
900
separates the argument list and the function's return type.
901 902

~~~~
903
fn line(a: int, b: int, x: int) -> int {
A
Andrew Paseltiner 已提交
904
    return a * x + b;
905 906 907 908 909
}
~~~~

The `return` keyword immediately returns from the body of a function. It
is optionally followed by an expression to return. A function can
910
also return a value by having its top-level block produce an
911 912 913
expression.

~~~~
914
fn line(a: int, b: int, x: int) -> int {
A
Andrew Paseltiner 已提交
915
    a * x + b
916 917 918
}
~~~~

919 920 921
It's better Rust style to write a return value this way instead of
writing an explicit `return`. The utility of `return` comes in when
returning early from a function. Functions that do not return a value
922
are said to return unit, `()`, and both the return type and the return
923 924
value may be omitted from the definition. The following two functions
are equivalent.
925 926 927 928 929 930 931

~~~~
fn do_nothing_the_hard_way() -> () { return (); }

fn do_nothing_the_easy_way() { }
~~~~

932 933 934
Ending the function with a semicolon like so is equivalent to returning `()`.

~~~~
A
Andrew Paseltiner 已提交
935 936
fn line(a: int, b: int, x: int) -> int { a * x + b  }
fn oops(a: int, b: int, x: int) -> ()  { a * x + b; }
937

P
Patrick Walton 已提交
938 939
assert!(8 == line(5, 3, 1));
assert!(() == oops(5, 3, 1));
940 941
~~~~

942 943
As with `match` expressions and `let` bindings, function arguments support
pattern destructuring. Like `let`, argument patterns must be irrefutable,
944
as in this example that unpacks the first value from a tuple and returns it.
945 946

~~~
D
Daniel Micay 已提交
947
fn first((value, _): (int, f64)) -> int { value }
948 949
~~~

950
# Destructors
B
Brian Anderson 已提交
951

952 953 954
A *destructor* is a function responsible for cleaning up the resources used by
an object when it is no longer accessible. Destructors can be defined to handle
the release of resources like files, sockets and heap memory.
955

C
Corey Richardson 已提交
956 957 958
Objects are never accessible after their destructor has been called, so no
dynamic failures are possible from accessing freed resources. When a task
fails, destructors of all objects in the task are called.
B
Brian Anderson 已提交
959

960
The `box` operator performs memory allocation on the heap:
961 962

~~~~
963 964
{
    // an integer allocated on the heap
965
    let y = box 10i;
966
}
967
// the destructor frees the heap memory as soon as `y` goes out of scope
968
~~~~
969

970 971 972
Rust includes syntax for heap memory allocation in the language since it's
commonly used, but the same semantics can be implemented by a type with a
custom destructor.
973

974 975 976 977 978 979 980 981 982
# Ownership

Rust formalizes the concept of object ownership to delegate management of an
object's lifetime to either a variable or a task-local garbage collector. An
object's owner is responsible for managing the lifetime of the object by
calling the destructor, and the owner determines whether the object is mutable.

Ownership is recursive, so mutability is inherited recursively and a destructor
destroys the contained tree of owned objects. Variables are top-level owners
D
Daniel Micay 已提交
983
and destroy the contained object when they go out of scope.
984

985 986
~~~~
// the struct owns the objects contained in the `x` and `y` fields
987
struct Foo { x: int, y: Box<int> }
988 989 990

{
    // `a` is the owner of the struct, and thus the owner of the struct's fields
991
    let a = Foo { x: 5, y: box 10 };
992
}
J
Jonathan Bailey 已提交
993
// when `a` goes out of scope, the destructor for the `Box<int>` in the struct's
994 995 996
// field is called

// `b` is mutable, and the mutability is inherited by the objects it owns
997
let mut b = Foo { x: 5, y: box 10 };
998 999 1000
b.x = 10;
~~~~

Y
Yazhong Liu 已提交
1001
If an object doesn't contain any non-`Send` types, it consists of a single
1002
ownership tree and is itself given the `Send` trait which allows it to be sent
1003
between tasks. Custom destructors can only be implemented directly on types
1004 1005 1006 1007
that are `Send`, but non-`Send` types can still *contain* types with custom
destructors. Example of types which are not `Send` are [`Gc<T>`][gc] and
[`Rc<T>`][rc], the shared-ownership types.

J
Jonas Hietala 已提交
1008 1009 1010 1011
> *Note:* See a [later chapter](#ownership-escape-hatches) for a discussion about
> [`Gc<T>`][gc] and [`Rc<T>`][rc], and the [chapter about traits](#traits) for
> a discussion about `Send`.

1012 1013
[gc]: http://doc.rust-lang.org/std/gc/struct.Gc.html
[rc]: http://doc.rust-lang.org/std/rc/struct.Rc.html
1014

D
Daniel Micay 已提交
1015 1016 1017 1018 1019 1020
# Implementing a linked list

An `enum` is a natural fit for describing a linked list, because it can express
a `List` type as being *either* the end of the list (`Nil`) or another node
(`Cons`). The full definition of the `Cons` variant will require some thought.

1021
~~~ {.ignore}
D
Daniel Micay 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030
enum List {
    Cons(...), // an incomplete definition of the next element in a List
    Nil        // the end of a List
}
~~~

The obvious approach is to define `Cons` as containing an element in the list
along with the next `List` node. However, this will generate a compiler error.

1031
~~~ {.ignore}
1032 1033
// error: illegal recursive enum type; wrap the inner value in a box to make it
// representable
D
Daniel Micay 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
enum List {
    Cons(u32, List), // an element (`u32`) and the next node in the list
    Nil
}
~~~

This error message is related to Rust's precise control over memory layout, and
solving it will require introducing the concept of *boxing*.

## Boxes
1044

1045
A value in Rust is stored directly inside the owner. If a `struct` contains
D
Daniel Micay 已提交
1046
four `u32` fields, it will be four times as large as a single `u32`.
1047

D
Daniel Micay 已提交
1048 1049 1050 1051 1052 1053 1054 1055
~~~
use std::mem::size_of; // bring `size_of` into the current scope, for convenience

struct Foo {
    a: u32,
    b: u32,
    c: u32,
    d: u32
1056 1057
}

D
Daniel Micay 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
assert_eq!(size_of::<Foo>(), size_of::<u32>() * 4);

struct Bar {
    a: Foo,
    b: Foo,
    c: Foo,
    d: Foo
}

assert_eq!(size_of::<Bar>(), size_of::<u32>() * 16);
~~~

Our previous attempt at defining the `List` type included an `u32` and a `List`
directly inside `Cons`, making it at least as big as the sum of both types. The
type was invalid because the size was infinite!

1074 1075 1076 1077
An *owned box* (`Box`, located in the `std::owned` module) uses a dynamic memory
allocation to provide the invariant of always being the size of a pointer,
regardless of the contained type. This can be leveraged to create a valid `List`
definition:
D
Daniel Micay 已提交
1078 1079

~~~
1080

D
Daniel Micay 已提交
1081
enum List {
1082
    Cons(u32, Box<List>),
D
Daniel Micay 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
    Nil
}
~~~

Defining a recursive data structure like this is the canonical example of an
owned box. Much like an unboxed value, an owned box has a single owner and is
therefore limited to expressing a tree-like data structure.

Consider an instance of our `List` type:

~~~
# enum List {
1095
#     Cons(u32, Box<List>),
D
Daniel Micay 已提交
1096 1097
#     Nil
# }
1098
let list = Cons(1, box Cons(2, box Cons(3, box Nil)));
D
Daniel Micay 已提交
1099 1100 1101 1102 1103 1104 1105
~~~

It represents an owned tree of values, inheriting mutability down the tree and
being destroyed along with the owner. Since the `list` variable above is
immutable, the whole list is immutable. The memory allocation itself is the
box, while the owner holds onto a pointer to it:

F
Florian Gilcher 已提交
1106
~~~text
1107 1108
            List box            List box            List box          List box
        +--------------+    +--------------+    +--------------+    +----------+
1109
list -> | Cons | 1 |   | -> | Cons | 2 |   | -> | Cons | 3 |   | -> | Nil      |
1110
        +--------------+    +--------------+    +--------------+    +----------+
1111
~~~
D
Daniel Micay 已提交
1112

1113
> *Note:* the above diagram shows the logical contents of the enum. The actual
1114 1115 1116 1117
> memory layout of the enum may vary. For example, for the `List` enum shown
> above, Rust guarantees that there will be no enum tag field in the actual
> structure. See the language reference for more details.

D
Daniel Micay 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
An owned box is a common example of a type with a destructor. The allocated
memory is cleaned up when the box is destroyed.

## Move semantics

Rust uses a shallow copy for parameter passing, assignment and returning from
functions. Passing around the `List` will copy only as deep as the pointer to
the box rather than doing an implicit heap allocation.

~~~
# enum List {
1129
#     Cons(u32, Box<List>),
D
Daniel Micay 已提交
1130 1131
#     Nil
# }
1132
let xs = Cons(1, box Cons(2, box Cons(3, box Nil)));
D
Daniel Micay 已提交
1133 1134 1135
let ys = xs; // copies `Cons(u32, pointer)` shallowly
~~~

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
> *Note:* Names like `xs` and `ys` are a naming
> convention for collection-like data structures
> (like our `List`). These collections are given 
> names appended with 's' to signify plurality, 
> i.e. that the data structure stores multiple 
> elements.  For example, `xs` in this case can 
> be read as "a list of ex-es", where "x" here 
> are elements of type `u32`.


D
Daniel Micay 已提交
1146 1147 1148 1149 1150 1151
Rust will consider a shallow copy of a type with a destructor like `List` to
*move ownership* of the value. After a value has been moved, the source
location cannot be used unless it is reinitialized.

~~~
# enum List {
1152
#     Cons(u32, Box<List>),
D
Daniel Micay 已提交
1153 1154 1155 1156 1157 1158 1159 1160 1161
#     Nil
# }
let mut xs = Nil;
let ys = xs;

// attempting to use `xs` will result in an error here

xs = Nil;

R
Randati 已提交
1162
// `xs` can be used again
D
Daniel Micay 已提交
1163 1164 1165 1166 1167
~~~

A destructor call will only occur for a variable that has not been moved from,
as it is only called a single time.

1168

D
Daniel Micay 已提交
1169
Avoiding a move can be done with the library-defined `clone` method:
1170 1171

~~~~
1172
let x = box 5i;
1173 1174
let y = x.clone(); // `y` is a newly allocated box
let z = x; // no new memory allocated, `x` can no longer be used
D
Daniel Micay 已提交
1175 1176 1177
~~~~

The `clone` method is provided by the `Clone` trait, and can be derived for
1178
our `List` type. Traits will be explained in detail [later](#traits).
D
Daniel Micay 已提交
1179

1180
~~~{.ignore}
D
Daniel Micay 已提交
1181 1182
#[deriving(Clone)]
enum List {
1183
    Cons(u32, Box<List>),
D
Daniel Micay 已提交
1184
    Nil
1185
}
D
Daniel Micay 已提交
1186

1187
let x = Cons(5, box Nil);
D
Daniel Micay 已提交
1188 1189 1190 1191 1192 1193
let y = x.clone();

// `x` can still be used!

let z = x;

T
Trent Ogren 已提交
1194
// and now, it can no longer be used since it has been moved
D
Daniel Micay 已提交
1195 1196 1197 1198 1199
~~~

The mutability of a value may be changed by moving it to a new owner:

~~~~
1200
let r = box 13i;
D
Daniel Micay 已提交
1201 1202 1203
let mut s = r; // box becomes mutable
*s += 1;
let t = s; // box becomes immutable
1204 1205
~~~~

D
Daniel Micay 已提交
1206 1207 1208 1209 1210
A simple way to define a function prepending to the `List` type is to take
advantage of moves:

~~~
enum List {
1211
    Cons(u32, Box<List>),
D
Daniel Micay 已提交
1212 1213 1214 1215
    Nil
}

fn prepend(xs: List, value: u32) -> List {
1216
    Cons(value, box xs)
D
Daniel Micay 已提交
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
}

let mut xs = Nil;
xs = prepend(xs, 1);
xs = prepend(xs, 2);
xs = prepend(xs, 3);
~~~

However, this is not a very flexible definition of `prepend` as it requires
ownership of a list to be passed in rather than just mutating it in-place.

## References

The obvious signature for a `List` equality comparison is the following:

1232
~~~{.ignore}
1233
fn eq(xs: List, ys: List) -> bool { /* ... */ }
D
Daniel Micay 已提交
1234 1235 1236 1237 1238 1239
~~~

However, this will cause both lists to be moved into the function. Ownership
isn't required to compare the lists, so the function should take *references*
(&T) instead.

1240
~~~{.ignore}
1241
fn eq(xs: &List, ys: &List) -> bool { /* ... */ }
D
Daniel Micay 已提交
1242 1243 1244 1245 1246 1247 1248 1249 1250
~~~

A reference is a *non-owning* view of a value. A reference can be obtained with the `&` (address-of)
operator. It can be dereferenced by using the `*` operator. In a pattern, such as `match` expression
branches, the `ref` keyword can be used to bind to a variable name by-reference rather than
by-value. A recursive definition of equality using references is as follows:

~~~
# enum List {
1251
#     Cons(u32, Box<List>),
D
Daniel Micay 已提交
1252 1253 1254 1255 1256 1257 1258
#     Nil
# }
fn eq(xs: &List, ys: &List) -> bool {
    // Match on the next node in both lists.
    match (xs, ys) {
        // If we have reached the end of both lists, they are equal.
        (&Nil, &Nil) => true,
1259
        // If the current elements of both lists are equal, keep going.
1260
        (&Cons(x, box ref next_xs), &Cons(y, box ref next_ys))
1261
                if x == y => eq(next_xs, next_ys),
D
Daniel Micay 已提交
1262 1263 1264 1265 1266
        // If the current elements are not equal, the lists are not equal.
        _ => false
    }
}

1267 1268
let xs = Cons(5, box Cons(10, box Nil));
let ys = Cons(5, box Cons(10, box Nil));
D
Daniel Micay 已提交
1269 1270 1271
assert!(eq(&xs, &ys));
~~~

1272
> *Note:* Rust doesn't guarantee [tail-call](http://en.wikipedia.org/wiki/Tail_call) optimization,
1273
> but LLVM is able to handle a simple case like this with optimizations enabled.
D
Daniel Micay 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282

## Lists of other types

Our `List` type is currently always a list of 32-bit unsigned integers. By
leveraging Rust's support for generics, it can be extended to work for any
element type.

The `u32` in the previous definition can be substituted with a type parameter:

1283
> *Note:* The following code introduces generics, which are explained in a
1284 1285
> [dedicated section](#generics).

D
Daniel Micay 已提交
1286 1287
~~~
enum List<T> {
1288
    Cons(T, Box<List<T>>),
D
Daniel Micay 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297
    Nil
}
~~~

The old `List` of `u32` is now available as `List<u32>`. The `prepend`
definition has to be updated too:

~~~
# enum List<T> {
1298
#     Cons(T, Box<List<T>>),
D
Daniel Micay 已提交
1299 1300 1301
#     Nil
# }
fn prepend<T>(xs: List<T>, value: T) -> List<T> {
1302
    Cons(value, box xs)
D
Daniel Micay 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
}
~~~

Generic functions and types like this are equivalent to defining specialized
versions for each set of type parameters.

Using the generic `List<T>` works much like before, thanks to type inference:

~~~
# enum List<T> {
1313
#     Cons(T, Box<List<T>>),
D
Daniel Micay 已提交
1314 1315 1316
#     Nil
# }
# fn prepend<T>(xs: List<T>, value: T) -> List<T> {
1317
#     Cons(value, box xs)
D
Daniel Micay 已提交
1318 1319
# }
let mut xs = Nil; // Unknown type! This is a `List<T>`, but `T` can be anything.
1320 1321 1322
xs = prepend(xs, 10i); // Here the compiler infers `xs`'s type as `List<int>`.
xs = prepend(xs, 15i);
xs = prepend(xs, 20i);
D
Daniel Micay 已提交
1323 1324 1325 1326 1327 1328 1329
~~~

The code sample above demonstrates type inference making most type annotations optional. It is
equivalent to the following type-annotated code:

~~~
# enum List<T> {
1330
#     Cons(T, Box<List<T>>),
D
Daniel Micay 已提交
1331 1332 1333
#     Nil
# }
# fn prepend<T>(xs: List<T>, value: T) -> List<T> {
1334
#     Cons(value, box xs)
D
Daniel Micay 已提交
1335 1336 1337 1338 1339 1340 1341
# }
let mut xs: List<int> = Nil::<int>;
xs = prepend::<int>(xs, 10);
xs = prepend::<int>(xs, 15);
xs = prepend::<int>(xs, 20);
~~~

1342 1343 1344
In declarations, the language uses `Type<T, U, V>` to describe a list of type
parameters, but expressions use `identifier::<T, U, V>`, to disambiguate the
`<` operator.
D
Daniel Micay 已提交
1345 1346 1347 1348 1349 1350 1351 1352

## Defining list equality with generics

Generic functions are type-checked from the definition, so any necessary properties of the type must
be specified up-front. Our previous definition of list equality relied on the element type having
the `==` operator available, and took advantage of the lack of a destructor on `u32` to copy it
without a move of ownership.

1353
We can add a *trait bound* on the `PartialEq` trait to require that the type implement the `==` operator.
D
Daniel Micay 已提交
1354 1355 1356 1357
Two more `ref` annotations need to be added to avoid attempting to move out the element types:

~~~
# enum List<T> {
1358
#     Cons(T, Box<List<T>>),
D
Daniel Micay 已提交
1359 1360
#     Nil
# }
1361
fn eq<T: PartialEq>(xs: &List<T>, ys: &List<T>) -> bool {
D
Daniel Micay 已提交
1362 1363 1364 1365
    // Match on the next node in both lists.
    match (xs, ys) {
        // If we have reached the end of both lists, they are equal.
        (&Nil, &Nil) => true,
1366
        // If the current elements of both lists are equal, keep going.
1367
        (&Cons(ref x, box ref next_xs), &Cons(ref y, box ref next_ys))
1368
                if x == y => eq(next_xs, next_ys),
D
Daniel Micay 已提交
1369 1370 1371 1372 1373
        // If the current elements are not equal, the lists are not equal.
        _ => false
    }
}

1374 1375
let xs = Cons('c', box Cons('a', box Cons('t', box Nil)));
let ys = Cons('c', box Cons('a', box Cons('t', box Nil)));
D
Daniel Micay 已提交
1376 1377 1378
assert!(eq(&xs, &ys));
~~~

1379 1380
This would be a good opportunity to implement the `PartialEq` trait for our list type, making the `==` and
`!=` operators available. We'll need to provide an `impl` for the `PartialEq` trait and a definition of the
D
Daniel Micay 已提交
1381 1382 1383 1384 1385
`eq` method. In a method, the `self` parameter refers to an instance of the type we're implementing
on.

~~~
# enum List<T> {
1386
#     Cons(T, Box<List<T>>),
D
Daniel Micay 已提交
1387 1388
#     Nil
# }
1389
impl<T: PartialEq> PartialEq for List<T> {
D
Daniel Micay 已提交
1390 1391 1392 1393 1394
    fn eq(&self, ys: &List<T>) -> bool {
        // Match on the next node in both lists.
        match (self, ys) {
            // If we have reached the end of both lists, they are equal.
            (&Nil, &Nil) => true,
1395
            // If the current elements of both lists are equal, keep going.
1396
            (&Cons(ref x, box ref next_xs), &Cons(ref y, box ref next_ys))
1397
                    if x == y => next_xs == next_ys,
D
Daniel Micay 已提交
1398 1399 1400 1401 1402 1403
            // If the current elements are not equal, the lists are not equal.
            _ => false
        }
    }
}

1404 1405
let xs = Cons(5i, box Cons(10i, box Nil));
let ys = Cons(5i, box Cons(10i, box Nil));
1406
// The methods below are part of the PartialEq trait,
1407
// which we implemented on our linked list.
D
Daniel Micay 已提交
1408 1409
assert!(xs.eq(&ys));
assert!(!xs.ne(&ys));
1410

1411
// The PartialEq trait also allows us to use the shorthand infix operators.
1412 1413
assert!(xs == ys);    // `xs == ys` is short for `xs.eq(&ys)`
assert!(!(xs != ys)); // `xs != ys` is short for `xs.ne(&ys)`
D
Daniel Micay 已提交
1414 1415 1416
~~~

# More on boxes
1417 1418 1419 1420 1421 1422 1423

The most common use case for owned boxes is creating recursive data structures
like a binary search tree. Rust's trait-based generics system (covered later in
the tutorial) is usually used for static dispatch, but also provides dynamic
dispatch via boxing. Values of different types may have different sizes, but a
box is able to *erase* the difference via the layer of indirection they
provide.
1424

1425 1426
In uncommon cases, the indirection can provide a performance gain or memory
reduction by making values smaller. However, unboxed values should almost
1427
always be preferred when they are usable.
1428

1429 1430 1431 1432 1433
Note that returning large unboxed values via boxes is unnecessary. A large
value is returned via a hidden output parameter, and the decision on where to
place the return value should be left to the caller:

~~~~
D
Daniel Micay 已提交
1434
fn foo() -> (u64, u64, u64, u64, u64, u64) {
1435 1436 1437
    (5, 5, 5, 5, 5, 5)
}

1438
let x = box foo(); // allocates a box, and writes the integers directly to it
1439 1440 1441 1442
~~~~

Beyond the properties granted by the size, an owned box behaves as a regular
value by inheriting the mutability and lifetime of the owner:
1443 1444

~~~~
1445 1446
let x = 5i; // immutable
let mut y = 5i; // mutable
1447 1448
y += 2;

1449 1450
let x = box 5i; // immutable
let mut y = box 5i; // mutable
1451
*y += 2; // the `*` operator is needed to access the contained value
1452 1453
~~~~

1454
# References
1455

1456
In contrast with
1457
owned boxes, where the holder of an owned box is the owner of the pointed-to
1458
memory, references never imply ownership - they are "borrowed".
1459
You can borrow a reference to
1460 1461
any object, and the compiler verifies that it cannot outlive the lifetime of
the object.
1462

1463
As an example, consider a simple struct type, `Point`:
1464

1465 1466
~~~
struct Point {
D
Daniel Micay 已提交
1467 1468
    x: f64,
    y: f64
1469
}
1470
~~~
1471

1472
We can use this simple definition to allocate points in many different
1473
ways. For example, in this code, each of these local variables
1474
contains a point, but allocated in a different location:
1475

1476
~~~
D
Daniel Micay 已提交
1477
# struct Point { x: f64, y: f64 }
1478
let on_the_stack :     Point  =     Point { x: 3.0, y: 4.0 };
C
Christoph Burgdorf 已提交
1479
let on_the_heap  : Box<Point> = box Point { x: 7.0, y: 9.0 };
1480
~~~
1481

J
Joris Rehm 已提交
1482
Suppose we want to write a procedure that computes the distance
1483
between any two points, no matter where they are stored. One option is
1484 1485 1486
to define a function that takes two arguments of type point—that is,
it takes the points by value. But this will cause the points to be
copied when we call the function. For points, this is probably not so
1487
bad, but often copies are expensive. So we’d like to define a function
1488
that takes the points by pointer. We can use references to do this:
1489

1490
~~~
D
Daniel Micay 已提交
1491 1492
# struct Point { x: f64, y: f64 }
fn compute_distance(p1: &Point, p2: &Point) -> f64 {
1493 1494
    let x_d = p1.x - p2.x;
    let y_d = p1.y - p2.y;
1495
    (x_d * x_d + y_d * y_d).sqrt()
1496
}
1497 1498 1499 1500 1501
~~~

Now we can call `compute_distance()` in various ways:

~~~
D
Daniel Micay 已提交
1502
# struct Point{ x: f64, y: f64 };
1503
# let on_the_stack :     Point  =     Point { x: 3.0, y: 4.0 };
C
Christoph Burgdorf 已提交
1504
# let on_the_heap  : Box<Point> = box Point { x: 7.0, y: 9.0 };
D
Daniel Micay 已提交
1505
# fn compute_distance(p1: &Point, p2: &Point) -> f64 { 0.0 }
1506
compute_distance(&on_the_stack, &*on_the_heap);
1507 1508 1509
~~~

Here the `&` operator is used to take the address of the variable
1510 1511
`on_the_stack`; this is because `on_the_stack` has the type `Point`
(that is, a struct value) and we have to take its address to get a
1512
reference. We also call this _borrowing_ the local variable
1513
`on_the_stack`, because we are creating an alias: that is, another
1514 1515
route to the same data.

D
DJUrsus 已提交
1516
Likewise, in the case of `on_the_heap`,
1517 1518
the `&` operator is used in conjunction with the `*` operator
to take a reference to the contents of the box.
1519 1520 1521 1522 1523 1524 1525

Whenever a value is borrowed, there are some limitations on what you
can do with the original. For example, if the contents of a variable
have been lent out, you cannot send that variable to another task, nor
will you be permitted to take actions that might cause the borrowed
value to be freed or to change its type. This rule should make
intuitive sense: you must wait for a borrowed value to be returned
1526
(that is, for the reference to go out of scope) before you can
1527 1528
make full use of it again.

1529 1530
For a more in-depth explanation of references and lifetimes, read the
[references and lifetimes guide][lifetimes].
1531

1532 1533
## Freezing

S
Steve Klabnik 已提交
1534 1535 1536 1537
Lending an &-pointer to an object freezes the pointed-to object and prevents
mutation—even if the object was declared as `mut`.  `Freeze` objects have
freezing enforced statically at compile-time. An example of a non-`Freeze` type
is [`RefCell<T>`][refcell].
1538 1539

~~~~
1540
let mut x = 5i;
1541
{
1542
    let y = &x; // `x` is now frozen. It cannot be modified or re-assigned.
1543
}
1544
// `x` is now unfrozen again
1545
# x = 3;
1546 1547
~~~~

1548
[refcell]: http://doc.rust-lang.org/std/cell/struct.RefCell.html
C
Corey Richardson 已提交
1549

1550
# Dereferencing pointers
1551 1552 1553 1554 1555

Rust uses the unary star operator (`*`) to access the contents of a
box or pointer, similarly to C.

~~~
1556 1557
let owned = box 10i;
let borrowed = &20i;
1558

1559
let sum = *owned + *borrowed;
1560 1561 1562
~~~

Dereferenced mutable pointers may appear on the left hand side of
1563 1564
assignments. Such an assignment modifies the value that the pointer
points to.
1565 1566

~~~
1567
let mut owned = box 10i;
1568

1569
let mut value = 20i;
1570 1571 1572
let borrowed = &mut value;

*owned = *borrowed + 100;
1573
*borrowed = *owned + 1000;
1574 1575 1576
~~~

Pointers have high operator precedence, but lower precedence than the
1577 1578
dot operator used for field and method access. This precedence order
can sometimes make code awkward and parenthesis-filled.
1579 1580

~~~
D
Daniel Micay 已提交
1581
# struct Point { x: f64, y: f64 }
1582
# enum Shape { Rectangle(Point, Point) }
1583
# impl Shape { fn area(&self) -> int { 0 } }
1584 1585
let start = box Point { x: 10.0, y: 20.0 };
let end = box Point { x: (*start).x + 100.0, y: (*start).y + 100.0 };
1586 1587 1588 1589
let rect = &Rectangle(*start, *end);
let area = (*rect).area();
~~~

1590
To combat this ugliness the dot operator applies _automatic pointer
1591
dereferencing_ to the receiver (the value on the left-hand side of the
1592
dot), so in most cases, explicitly dereferencing the receiver is not necessary.
1593 1594

~~~
D
Daniel Micay 已提交
1595
# struct Point { x: f64, y: f64 }
1596
# enum Shape { Rectangle(Point, Point) }
1597
# impl Shape { fn area(&self) -> int { 0 } }
1598 1599
let start = box Point { x: 10.0, y: 20.0 };
let end = box Point { x: start.x + 100.0, y: start.y + 100.0 };
1600 1601 1602 1603
let rect = &Rectangle(*start, *end);
let area = rect.area();
~~~

1604
You can write an expression that dereferences any number of pointers
J
Joris Rehm 已提交
1605
automatically. For example, if you feel inclined, you could write
1606
something silly like
1607 1608

~~~
D
Daniel Micay 已提交
1609
# struct Point { x: f64, y: f64 }
1610
let point = &box Point { x: 10.0, y: 20.0 };
A
Alex Crichton 已提交
1611
println!("{:f}", point.x);
1612 1613
~~~

1614
The indexing operator (`[]`) also auto-dereferences.
1615

1616
# Vectors and strings
1617

1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
A vector is a contiguous block of memory containing zero or more values of the
same type. Rust also supports vector reference types, called slices, which are
a view into a block of memory represented as a pointer and a length.

Strings are represented as vectors of `u8`, with the guarantee of containing a
valid UTF-8 sequence.

Fixed-size vectors are an unboxed block of memory, with the element length as
part of the type. A fixed-size vector owns the elements it contains, so the
elements are mutable if the vector is mutable. Fixed-size strings do not exist.
1628 1629

~~~
1630
// A fixed-size vector
1631
let numbers = [1i, 2, 3];
1632 1633 1634 1635
let more_numbers = numbers;

// The type of a fixed-size vector is written as `[Type, ..length]`
let five_zeroes: [int, ..5] = [0, ..5];
1636
~~~
1637

1638 1639 1640
A unique vector is dynamically sized, and has a destructor to clean up
allocated memory on the heap. A unique vector owns the elements it contains, so
the elements are mutable if the vector is mutable.
1641

1642
~~~
1643
use std::string::String;
1644

1645
// A dynamically sized vector (unique vector)
1646
let mut numbers = vec![1i, 2, 3];
1647 1648
numbers.push(4);
numbers.push(5);
1649

K
Kevin Ballard 已提交
1650 1651
// The type of a unique vector is written as `Vec<int>`
let more_numbers: Vec<int> = numbers.move_iter().map(|i| i+1).collect();
1652

1653
// The original `numbers` value can no longer be used, due to move semantics.
1654

1655
let mut string = String::from_str("fo");
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
string.push_char('o');
~~~

Slices are similar to fixed-size vectors, but the length is not part of the
type. They simply point into a block of memory and do not have ownership over
the elements.

~~~
// A slice
let xs = &[1, 2, 3];

1667
// Slices have their type written as `&[int]`
1668
let ys: &[int] = xs;
1669

1670 1671 1672 1673 1674 1675 1676
// Other vector types coerce to slices
let three = [1, 2, 3];
let zs: &[int] = three;

// An unadorned string literal is an immutable string slice
let string = "foobar";

1677
// A string slice type is written as `&str`
1678 1679 1680
let view: &str = string.slice(0, 3);
~~~

J
Jonas Hietala 已提交
1681 1682 1683 1684 1685 1686 1687
Square brackets denote indexing into a slice or fixed-size vector:

~~~~
let crayons: [&str, ..3] = ["BananaMania", "Beaver", "Bittersweet"];
println!("Crayon 2 is '{}'", crayons[2]);
~~~~

1688 1689 1690 1691 1692 1693
Mutable slices also exist, just as there are mutable references. However, there
are no mutable string slices. Strings are a multi-byte encoding (UTF-8) of
Unicode code points, so they cannot be freely mutated without the ability to
alter the length.

~~~
1694
let mut xs = [1i, 2i, 3i];
1695 1696 1697
let view = xs.mut_slice(0, 2);
view[0] = 5;

1698
// The type of a mutable slice is written as `&mut [T]`
1699
let ys: &mut [int] = &mut [1i, 2i, 3i];
1700
~~~
1701

K
Kevin Ballard 已提交
1702
A slice or fixed-size vector can be destructured using pattern matching:
1703 1704

~~~~
1705
let numbers: &[int] = &[1, 2, 3];
1706 1707 1708 1709 1710 1711 1712 1713
let score = match numbers {
    [] => 0,
    [a] => a * 10,
    [a, b] => a * 6 + b * 4,
    [a, b, c, ..rest] => a * 5 + b * 3 + c * 2 + rest.len() as int
};
~~~~

1714
Both vectors and strings support a number of useful [methods](#methods),
K
Kevin Ballard 已提交
1715
defined in [`std::vec`], [`std::slice`], and [`std::str`].
1716

1717
[`std::vec`]: std/vec/index.html
K
Kevin Ballard 已提交
1718
[`std::slice`]: std/slice/index.html
1719
[`std::str`]: std/str/index.html
1720

D
Daniel Micay 已提交
1721 1722
# Ownership escape hatches

B
Brian Anderson 已提交
1723
Ownership can cleanly describe tree-like data structures, and references provide non-owning pointers. However, more flexibility is often desired and Rust provides ways to escape from strict
D
Daniel Micay 已提交
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
single parent ownership.

The standard library provides the `std::rc::Rc` pointer type to express *shared ownership* over a
reference counted box. As soon as all of the `Rc` pointers go out of scope, the box and the
contained value are destroyed.

~~~
use std::rc::Rc;

// A fixed-size array allocated in a reference-counted box
1734
let x = Rc::new([1i, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
D
Daniel Micay 已提交
1735 1736 1737
let y = x.clone(); // a new owner
let z = x; // this moves `x` into `z`, rather than creating a new owner

1738
assert!(*z == [1i, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
D
Daniel Micay 已提交
1739

1740 1741
// the variable is mutable, but not the contents of the box
let mut a = Rc::new([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
D
Daniel Micay 已提交
1742 1743 1744 1745 1746 1747 1748 1749
a = z;
~~~

A garbage collected pointer is provided via `std::gc::Gc`, with a task-local garbage collector
having ownership of the box. It allows the creation of cycles, and the individual `Gc` pointers do
not have a destructor.

~~~
1750
use std::gc::GC;
D
Daniel Micay 已提交
1751 1752

// A fixed-size array allocated in a garbage-collected box
1753
let x = box(GC) [1i, 2, 3, 4, 5, 6, 7, 8, 9, 10];
D
Daniel Micay 已提交
1754 1755 1756
let y = x; // does not perform a move, unlike with `Rc`
let z = x;

1757
assert!(*z == [1i, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
D
Daniel Micay 已提交
1758 1759 1760 1761 1762 1763 1764
~~~

With shared ownership, mutability cannot be inherited so the boxes are always immutable. However,
it's possible to use *dynamic* mutability via types like `std::cell::Cell` where freezing is handled
via dynamic checks and can fail at runtime.

The `Rc` and `Gc` types are not sendable, so they cannot be used to share memory between tasks. Safe
J
Jonathan Bailey 已提交
1765
immutable and mutable shared memory is provided by the `sync::arc` module.
D
Daniel Micay 已提交
1766

J
Jonas Hietala 已提交
1767 1768
> *Note:* See a [later chapter](#traits) for a discussion about `Send` and sendable types.

1769 1770
# Closures

B
Brian Anderson 已提交
1771
Named functions, like those we've seen so far, may not refer to local
1772 1773 1774
variables declared outside the function: they do not close over their
environment (sometimes referred to as "capturing" variables in their
environment). For example, you couldn't write the following:
1775 1776

~~~~ {.ignore}
1777
let x = 3;
1778

1779 1780
// `fun` cannot refer to `x`
fn fun() -> () { println!("{}", x); }
1781 1782
~~~~

M
mdinger 已提交
1783 1784 1785
A _closure_ does support accessing the enclosing scope; below we will create
2 _closures_ (nameless functions). Compare how `||` replaces `()` and how
they try to access `x`:
1786

M
mdinger 已提交
1787
~~~~ {.ignore}
1788
let x = 3;
1789

1790
// `fun` is an invalid definition
1791 1792
fn  fun       () -> () { println!("{}", x) }  // cannot capture from enclosing scope
let closure = || -> () { println!("{}", x) }; // can capture from enclosing scope
1793

M
mdinger 已提交
1794
// `fun_arg` is an invalid definition
1795 1796 1797
fn  fun_arg       (arg: int) -> () { println!("{}", arg + x) }  // cannot capture
let closure_arg = |arg: int| -> () { println!("{}", arg + x) }; // can capture
//                      ^
M
mdinger 已提交
1798 1799
// Requires a type because the implementation needs to know which `+` to use.
// In the future, the implementation may not need the help.
1800

M
mdinger 已提交
1801 1802
fun();          // Still won't work
closure();      // Prints: 3
1803

M
mdinger 已提交
1804 1805
fun_arg(7);     // Still won't work
closure_arg(7); // Prints: 10
1806 1807
~~~~

1808
Closures begin with the argument list between vertical bars and are followed by
1809 1810 1811
a single expression. Remember that a block, `{ <expr1>; <expr2>; ... }`, is
considered a single expression: it evaluates to the result of the last
expression it contains if that expression is not followed by a semicolon,
1812
otherwise the block evaluates to `()`, the unit value.
1813

1814 1815 1816 1817 1818
In general, return types and all argument types must be specified
explicitly for function definitions.  (As previously mentioned in the
[Functions section](#functions), omitting the return type from a
function declaration is synonymous with an explicit declaration of
return type unit, `()`.)
1819

M
mdinger 已提交
1820
~~~~ {.ignore}
1821 1822
fn  fun   (x: int)         { println!("{}", x) } // this is same as saying `-> ()`
fn  square(x: int) -> uint { (x * x) as uint }   // other return types are explicit
1823

1824 1825
// Error: mismatched types: expected `()` but found `uint`
fn  badfun(x: int)         { (x * x) as uint }
1826
~~~~
1827

1828 1829 1830 1831 1832 1833
On the other hand, the compiler can usually infer both the argument
and return types for a closure expression; therefore they are often
omitted, since both a human reader and the compiler can deduce the
types from the immediate context.  This is in contrast to function
declarations, which require types to be specified and are not subject
to type inference. Compare:
1834

1835 1836 1837 1838 1839 1840 1841
~~~~ {.ignore}
// `fun` as a function declaration cannot infer the type of `x`, so it must be provided
fn  fun       (x: int) { println!("{}", x) }
let closure = |x     | { println!("{}", x) }; // infers `x: int`, return type `()`

// For closures, omitting a return type is *not* synonymous with `-> ()`
let add_3   = |y     | { 3i + y }; // infers `y: int`, return type `int`.
M
mdinger 已提交
1842

1843 1844 1845
fun(10);            // Prints 10
closure(20);        // Prints 20
closure(add_3(30)); // Prints 33
1846

1847 1848 1849 1850 1851
fun("String"); // Error: mismatched types

// Error: mismatched types
// inference already assigned `closure` the type `|int| -> ()`
closure("String");
1852 1853
~~~~

1854 1855 1856 1857 1858 1859 1860 1861 1862
In cases where the compiler needs assistance, the arguments and return
types may be annotated on closures, using the same notation as shown
earlier.  In the example below, since different types provide an
implementation for the operator `*`, the argument type for the `x`
parameter must be explicitly provided.

~~~~{.ignore}
// Error: the type of `x` must be known to be used with `x * x`
let square = |x     | -> uint { (x * x) as uint };
M
mdinger 已提交
1863
~~~~
1864

1865 1866
In the corrected version, the argument type is explicitly annotated,
while the return type can still be inferred.
1867 1868

~~~~
1869 1870
let square_explicit = |x: int| -> uint { (x * x) as uint };
let square_infer    = |x: int|         { (x * x) as uint };
M
mdinger 已提交
1871

1872 1873
println!("{}", square_explicit(20));  // 400
println!("{}", square_infer(-20));    // 400
B
Brian Anderson 已提交
1874 1875 1876
~~~~

There are several forms of closure, each with its own role. The most
1877
common, called a _stack closure_, has type `||` and can directly
1878
access local variables in the enclosing scope.
B
Brian Anderson 已提交
1879 1880 1881

~~~~
let mut max = 0;
1882 1883 1884 1885
let f = |x: int| if x > max { max = x };
for x in [1, 2, 3].iter() {
    f(*x);
}
1886 1887
~~~~

B
Brian Anderson 已提交
1888 1889 1890
Stack closures are very efficient because their environment is
allocated on the call stack and refers by pointer to captured
locals. To ensure that stack closures never outlive the local
1891 1892 1893 1894
variables to which they refer, stack closures are not
first-class. That is, they can only be used in argument position; they
cannot be stored in data structures or returned from
functions. Despite these limitations, stack closures are used
B
Brian Anderson 已提交
1895
pervasively in Rust code.
1896

1897
## Owned closures
1898

1899
Owned closures, written `proc`,
B
Brian Anderson 已提交
1900
hold on to things that can safely be sent between
1901 1902
processes. They copy the values they close over,
but they also own them: that is, no other code can access
1903
them. Owned closures are used in concurrent code, particularly
T
Tim Chevalier 已提交
1904 1905
for spawning [tasks][tasks].

1906 1907 1908 1909 1910 1911 1912 1913 1914
Closures can be used to spawn tasks.
A practical example of this pattern is found when using the `spawn` function,
which starts a new task.

~~~~
use std::task::spawn;

// proc is the closure which will be spawned.
spawn(proc() {
1915
    println!("I'm a new task")
1916 1917 1918
});
~~~~

1919
## Closure compatibility
1920

1921
Rust closures have a convenient subtyping property: you can pass any kind of
1922
closure (as long as the arguments and return types match) to functions
1923
that expect a `||`. Thus, when writing a higher-order function that
1924
only calls its function argument, and does nothing else with it, you
1925
should almost always declare the type of that argument as `||`. That way,
1926
callers may pass any kind of closure.
1927 1928

~~~~
1929
fn call_twice(f: ||) { f(); f(); }
1930 1931 1932 1933
let closure = || { "I'm a closure, and it doesn't matter what type I am"; };
fn function() { "I'm a normal function"; }
call_twice(closure);
call_twice(function);
1934 1935
~~~~

1936
> *Note:* Both the syntax and the semantics will be changing
1937
> in small ways. At the moment they can be unsound in some
1938 1939
> scenarios, particularly with non-copyable types.

1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
# Methods

Methods are like functions except that they always begin with a special argument,
called `self`,
which has the type of the method's receiver. The
`self` argument is like `this` in C++ and many other languages.
Methods are called with dot notation, as in `my_vec.len()`.

_Implementations_, written with the `impl` keyword, can define
methods on most Rust types, including structs and enums.
As an example, let's define a `draw` method on our `Shape` enum.

~~~
D
Daniel Micay 已提交
1953
# fn draw_circle(p: Point, f: f64) { }
1954 1955
# fn draw_rectangle(p: Point, p: Point) { }
struct Point {
D
Daniel Micay 已提交
1956 1957
    x: f64,
    y: f64
1958 1959 1960
}

enum Shape {
D
Daniel Micay 已提交
1961
    Circle(Point, f64),
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
    Rectangle(Point, Point)
}

impl Shape {
    fn draw(&self) {
        match *self {
            Circle(p, f) => draw_circle(p, f),
            Rectangle(p1, p2) => draw_rectangle(p1, p2)
        }
    }
}

D
Daniel Micay 已提交
1974
let s = Circle(Point { x: 1.0, y: 2.0 }, 3.0);
1975 1976 1977 1978 1979 1980 1981 1982 1983
s.draw();
~~~

This defines an _implementation_ for `Shape` containing a single
method, `draw`. In most respects the `draw` method is defined
like any other function, except for the name `self`.

The type of `self` is the type on which the method is implemented,
or a pointer thereof. As an argument it is written either `self`,
1984
`&self`, or `self: TYPE`.
1985 1986 1987
A caller must in turn have a compatible pointer type to call the method.

~~~
D
Daniel Micay 已提交
1988
# fn draw_circle(p: Point, f: f64) { }
1989
# fn draw_rectangle(p: Point, p: Point) { }
D
Daniel Micay 已提交
1990
# struct Point { x: f64, y: f64 }
1991
# enum Shape {
D
Daniel Micay 已提交
1992
#     Circle(Point, f64),
1993 1994 1995
#     Rectangle(Point, Point)
# }
impl Shape {
1996
    fn draw_reference(&self) { /* ... */ }
1997
    fn draw_owned(self: Box<Shape>) { /* ... */ }
1998
    fn draw_value(self) { /* ... */ }
1999 2000
}

D
Daniel Micay 已提交
2001
let s = Circle(Point { x: 1.0, y: 2.0 }, 3.0);
2002

2003
(&s).draw_reference();
2004
(box s).draw_owned();
2005 2006 2007
s.draw_value();
~~~

2008
Methods typically take a reference self type,
2009
so the compiler will go to great lengths to convert a callee
2010
to a reference.
2011 2012

~~~
D
Daniel Micay 已提交
2013
# fn draw_circle(p: Point, f: f64) { }
2014
# fn draw_rectangle(p: Point, p: Point) { }
D
Daniel Micay 已提交
2015
# struct Point { x: f64, y: f64 }
2016
# enum Shape {
D
Daniel Micay 已提交
2017
#     Circle(Point, f64),
2018 2019 2020
#     Rectangle(Point, Point)
# }
# impl Shape {
2021
#    fn draw_reference(&self) { /* ... */ }
2022
#    fn draw_owned(self: Box<Shape>) { /* ... */ }
2023
#    fn draw_value(self) { /* ... */ }
2024
# }
D
Daniel Micay 已提交
2025
# let s = Circle(Point { x: 1.0, y: 2.0 }, 3.0);
2026
// As with typical function arguments, owned pointers
2027
// are automatically converted to references
2028

2029
(box s).draw_reference();
2030 2031 2032

// Unlike typical function arguments, the self value will
// automatically be referenced ...
2033
s.draw_reference();
2034 2035

// ... and dereferenced
2036
(& &s).draw_reference();
2037

2038
// ... and dereferenced and borrowed
2039
(&box s).draw_reference();
2040 2041
~~~

2042
Implementations may also define standalone (sometimes called "static")
J
Joris Rehm 已提交
2043
methods. The absence of a `self` parameter distinguishes such methods.
2044
These methods are the preferred way to define constructor functions.
2045

2046
~~~~ {.ignore}
2047
impl Circle {
2048 2049
    fn area(&self) -> f64 { /* ... */ }
    fn new(area: f64) -> Circle { /* ... */ }
2050 2051 2052
}
~~~~

2053
To call such a method, just prefix it with the type name and a double colon:
2054 2055

~~~~
2056
use std::f64::consts::PI;
D
Daniel Micay 已提交
2057
struct Circle { radius: f64 }
2058
impl Circle {
2059
    fn new(area: f64) -> Circle { Circle { radius: (area / PI).sqrt() } }
2060 2061 2062 2063
}
let c = Circle::new(42.5);
~~~~

2064 2065
# Generics

2066 2067 2068 2069 2070 2071 2072
Throughout this tutorial, we've been defining functions that act only
on specific data types. With type parameters we can also define
functions whose arguments have generic types, and which can be invoked
with a variety of types. Consider a generic `map` function, which
takes a function `function` and a vector `vector` and returns a new
vector consisting of the result of applying `function` to each element
of `vector`:
2073 2074

~~~~
2075 2076
fn map<T, U>(vector: &[T], function: |v: &T| -> U) -> Vec<U> {
    let mut accumulator = Vec::new();
2077
    for element in vector.iter() {
2078
        accumulator.push(function(element));
2079
    }
2080
    return accumulator;
2081 2082 2083
}
~~~~

2084 2085
When defined with type parameters, as denoted by `<T, U>`, this
function can be applied to any type of vector, as long as the type of
2086
`function`'s argument and the type of the vector's contents agree with
2087
each other.
2088

2089
Inside a generic function, the names of the type parameters
2090 2091 2092
(capitalized by convention) stand for opaque types. All you can do
with instances of these types is pass them around: you can't apply any
operations to them or pattern-match on them. Note that instances of
2093
generic types are often passed by pointer. For example, the parameter
2094
`function()` is supplied with a pointer to a value of type `T` and not
2095
a value of type `T` itself. This ensures that the function works with
2096 2097
the broadest set of types possible, since some types are expensive or
illegal to copy and pass by value.
2098

2099
Generic `type`, `struct`, and `enum` declarations follow the same pattern:
2100 2101

~~~~
2102
type Set<T> = std::collections::HashMap<T, ()>;
2103

2104
struct Stack<T> {
2105
    elements: Vec<T>
2106
}
2107

2108 2109 2110
enum Option<T> {
    Some(T),
    None
2111
}
2112
# fn main() {}
2113 2114
~~~~

2115
These declarations can be instantiated to valid types like `Set<int>`,
2116
`Stack<int>`, and `Option<int>`.
2117 2118 2119 2120 2121 2122 2123 2124

The last type in that example, `Option`, appears frequently in Rust code.
Because Rust does not have null pointers (except in unsafe code), we need
another way to write a function whose result isn't defined on every possible
combination of arguments of the appropriate types. The usual way is to write
a function that returns `Option<T>` instead of `T`.

~~~~
D
Daniel Micay 已提交
2125 2126 2127
# struct Point { x: f64, y: f64 }
# enum Shape { Circle(Point, f64), Rectangle(Point, Point) }
fn radius(shape: Shape) -> Option<f64> {
2128 2129
    match shape {
        Circle(_, radius) => Some(radius),
2130
        Rectangle(..)     => None
2131
    }
2132 2133
}
~~~~
2134

2135 2136
The Rust compiler compiles generic functions very efficiently by
*monomorphizing* them. *Monomorphization* is a fancy name for a simple
B
Brian Anderson 已提交
2137 2138
idea: generate a separate copy of each generic function at each call site,
a copy that is specialized to the argument
2139 2140 2141
types and can thus be optimized specifically for them. In this
respect, Rust's generics have similar performance characteristics to
C++ templates.
2142

2143
## Traits
2144

2145 2146
Within a generic function—that is, a function parameterized by a
type parameter, say, `T`—the operations we can do on arguments of
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
type `T` are quite limited.  After all, since we don't know what type
`T` will be instantiated with, we can't safely modify or query values
of type `T`.  This is where _traits_ come into play. Traits are Rust's
most powerful tool for writing polymorphic code. Java developers will
see them as similar to Java interfaces, and Haskellers will notice
their similarities to type classes. Rust's traits give us a way to
express *bounded polymorphism*: by limiting the set of possible types
that a type parameter could refer to, they expand the number of
operations we can safely perform on arguments of that type.

As motivation, let us consider copying of values in Rust.  The `clone`
method is not defined for values of every type.  One reason is
user-defined destructors: copying a value of a type that has a
destructor could result in the destructor running multiple times.
Therefore, values of types that have destructors cannot be copied
unless we explicitly implement `clone` for them.
P
Patrick Walton 已提交
2163 2164

This complicates handling of generic functions.
2165 2166 2167 2168
If we have a function with a type parameter `T`,
can we copy values of type `T` inside that function?
In Rust, we can't,
and if we try to run the following code the compiler will complain.
2169

2170
~~~~ {.ignore}
2171
// This does not compile
2172 2173
fn head_bad<T>(v: &[T]) -> T {
    v[0] // error: copying a non-copyable value
2174 2175 2176
}
~~~~

P
Patrick Walton 已提交
2177
However, we can tell the compiler
2178
that the `head` function is only for copyable types.
2179
In Rust, copyable types are those that _implement the `Clone` trait_.
2180 2181
We can then explicitly create a second copy of the value we are returning
by calling the `clone` method:
2182

2183
~~~~
2184
// This does
P
Patrick Walton 已提交
2185 2186
fn head<T: Clone>(v: &[T]) -> T {
    v[0].clone()
2187
}
2188 2189
~~~~

2190 2191 2192 2193
The bounded type parameter `T: Clone` says that `head`
can be called on an argument of type `&[T]` for any `T`,
so long as there is an implementation of the
`Clone` trait for `T`.
P
Patrick Walton 已提交
2194
When instantiating a generic function,
2195
we can only instantiate it with types
P
Patrick Walton 已提交
2196
that implement the correct trait,
2197
so we could not apply `head` to a vector whose elements are of some type
P
Patrick Walton 已提交
2198
that does not implement `Clone`.
2199

P
Patrick Walton 已提交
2200
While most traits can be defined and implemented by user code,
2201
three traits are automatically derived and implemented
P
Patrick Walton 已提交
2202 2203
for all applicable types by the compiler,
and may not be overridden:
2204

P
Patrick Walton 已提交
2205 2206
* `Send` - Sendable types.
Types are sendable
2207
unless they contain references.
2208

A
Alex Crichton 已提交
2209
* `Sync` - Types that are *threadsafe*.
F
Flavio Percoco 已提交
2210
These are types that are safe to be used across several threads with access to
2211
a `&T` pointer. `Mutex<T>` is an example of a *sharable* type with internal mutable data.
F
Flavio Percoco 已提交
2212

2213 2214 2215
* `'static` - Non-borrowed types.
These are types that do not contain any data whose lifetime is bound to
a particular stack frame. These are types that do not contain any
2216
references, or types where the only contained references
2217
have the `'static` lifetime. (For more on named lifetimes and their uses,
N
noam 已提交
2218
see the [references and lifetimes guide][lifetimes].)
2219

2220
> *Note:* These built-in traits were referred to as 'kinds' in earlier
2221
> iterations of the language, and often still are.
2222

2223
Additionally, the `Drop` trait is used to define destructors. This
2224
trait provides one method called `drop`, which is automatically
I
Isaac Aggrey 已提交
2225
called when a value of the type that implements this trait is
2226 2227
destroyed, either because the value went out of scope or because the
garbage collector reclaimed it.
2228 2229 2230

~~~
struct TimeBomb {
2231
    explosivity: uint
2232 2233
}

2234
impl Drop for TimeBomb {
D
Daniel Micay 已提交
2235
    fn drop(&mut self) {
D
Daniel Micay 已提交
2236
        for _ in range(0, self.explosivity) {
2237
            println!("blam!");
2238 2239 2240 2241 2242
        }
    }
}
~~~

L
Luqman Aden 已提交
2243
It is illegal to call `drop` directly. Only code inserted by the compiler
2244 2245
may call it.

2246
## Declaring and implementing traits
2247

2248
At its simplest, a trait is a set of zero or more _method signatures_.
P
Patrick Walton 已提交
2249 2250
For example, we could declare the trait
`Printable` for things that can be printed to the console,
2251
with a single method signature:
2252 2253

~~~~
2254
trait Printable {
2255
    fn print(&self);
2256 2257 2258
}
~~~~

2259 2260 2261 2262
We say that the `Printable` trait _provides_ a `print` method with the
given signature.  This means that we can call `print` on an argument
of any type that implements the `Printable` trait.

A
Alex Crichton 已提交
2263
Rust's built-in `Send` and `Sync` types are examples of traits that
2264 2265 2266
don't provide any methods.

Traits may be implemented for specific types with [impls]. An impl for
2267 2268
a particular trait gives an implementation of the methods that
trait provides.  For instance, the following impls of
2269
`Printable` for `int` and `String` give implementations of the `print`
2270
method.
B
Brian Anderson 已提交
2271

G
gifnksm 已提交
2272
[impls]: #methods
2273 2274

~~~~
2275
# trait Printable { fn print(&self); }
2276
impl Printable for int {
2277
    fn print(&self) { println!("{}", *self) }
2278 2279
}

2280
impl Printable for String {
2281
    fn print(&self) { println!("{}", *self) }
2282 2283 2284
}

# 1.print();
2285
# ("foo".to_string()).print();
2286 2287 2288 2289 2290 2291 2292 2293 2294
~~~~

Methods defined in an impl for a trait may be called just like
any other method, using dot notation, as in `1.print()`.

## Default method implementations in trait definitions

Sometimes, a method that a trait provides will have the same
implementation for most or all of the types that implement that trait.
2295
For instance, suppose that we wanted `bool`s and `f32`s to be
2296 2297 2298 2299
printable, and that we wanted the implementation of `print` for those
types to be exactly as it is for `int`, above:

~~~~
2300
# trait Printable { fn print(&self); }
2301
impl Printable for f32 {
2302
    fn print(&self) { println!("{}", *self) }
2303 2304 2305
}

impl Printable for bool {
2306
    fn print(&self) { println!("{}", *self) }
2307
}
2308

2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
# true.print();
# 3.14159.print();
~~~~

This works fine, but we've now repeated the same definition of `print`
in three places.  Instead of doing that, we can simply include the
definition of `print` right in the trait definition, instead of just
giving its signature.  That is, we can write the following:

~~~~
2319 2320 2321
extern crate debug;

# fn main() {
2322
trait Printable {
2323
    // Default method implementation
2324 2325 2326 2327 2328
    fn print(&self) { println!("{:?}", *self) }
}

impl Printable for int {}

2329
impl Printable for String {
2330
    fn print(&self) { println!("{}", *self) }
2331
}
2332

2333 2334
impl Printable for bool {}

2335
impl Printable for f32 {}
2336

2337
# 1.print();
2338
# ("foo".to_string()).print();
2339 2340
# true.print();
# 3.14159.print();
2341
# }
2342 2343
~~~~

2344
Here, the impls of `Printable` for `int`, `bool`, and `f32` don't
2345 2346 2347 2348 2349 2350
need to provide an implementation of `print`, because in the absence
of a specific implementation, Rust just uses the _default method_
provided in the trait definition.  Depending on the trait, default
methods can save a great deal of boilerplate code from having to be
written in impls.  Of course, individual impls can still override the
default method for `print`, as is being done above in the impl for
2351
`String`.
2352 2353 2354 2355 2356

## Type-parameterized traits

Traits may be parameterized by type variables.  For example, a trait
for generalized sequence types might look like the following:
2357 2358

~~~~
2359
trait Seq<T> {
D
Daniel Micay 已提交
2360
    fn length(&self) -> uint;
2361
}
2362

2363
impl<T> Seq<T> for Vec<T> {
D
Daniel Micay 已提交
2364
    fn length(&self) -> uint { self.len() }
2365 2366 2367
}
~~~~

2368 2369 2370
The implementation has to explicitly declare the type parameter that
it binds, `T`, before using it to specify its trait type. Rust
requires this declaration because the `impl` could also, for example,
2371
specify an implementation of `Seq<int>`. The trait type (appearing
2372
between `impl` and `for`) *refers* to a type, rather than
2373
defining one.
2374 2375 2376

The type parameters bound by a trait are in scope in each of the
method declarations. So, re-declaring the type parameter
A
Adolfo Ochagavía 已提交
2377
`T` as an explicit type parameter for `length`, in either the trait or
2378
the impl, would be a compile-time error.
2379

I
ILYONG CHO 已提交
2380
Within a trait definition, `Self` is a special type that you can think
B
Brian Anderson 已提交
2381
of as a type parameter. An implementation of the trait for any given
I
ILYONG CHO 已提交
2382 2383
type `T` replaces the `Self` type parameter with `T`. The following
trait describes types that support an equality operation:
B
Brian Anderson 已提交
2384 2385

~~~~
2386 2387
// In a trait, `self` refers to the self argument.
// `Self` refers to the type implementing the trait.
2388
trait PartialEq {
2389
    fn equals(&self, other: &Self) -> bool;
B
Brian Anderson 已提交
2390 2391
}

2392
// In an impl, `self` refers just to the value of the receiver
2393
impl PartialEq for int {
2394
    fn equals(&self, other: &int) -> bool { *other == *self }
B
Brian Anderson 已提交
2395 2396 2397
}
~~~~

2398
Notice that in the trait definition, `equals` takes a
I
ILYONG CHO 已提交
2399
second parameter of type `Self`.
2400 2401
In contrast, in the `impl`, `equals` takes a second parameter of
type `int`, only using `self` as the name of the receiver.
B
Brian Anderson 已提交
2402

2403 2404 2405 2406
Just as in type implementations, traits can define standalone (static)
methods.  These methods are called by prefixing the method name with the trait
name and a double colon.  The compiler uses type inference to decide which
implementation to use.
2407 2408

~~~~
2409
use std::f64::consts::PI;
D
Daniel Micay 已提交
2410 2411 2412
trait Shape { fn new(area: f64) -> Self; }
struct Circle { radius: f64 }
struct Square { length: f64 }
2413

2414
impl Shape for Circle {
2415
    fn new(area: f64) -> Circle { Circle { radius: (area / PI).sqrt() } }
2416
}
2417
impl Shape for Square {
2418
    fn new(area: f64) -> Square { Square { length: area.sqrt() } }
2419 2420 2421 2422 2423 2424 2425
}

let area = 42.5;
let c: Circle = Shape::new(area);
let s: Square = Shape::new(area);
~~~~

2426
## Bounded type parameters and static method dispatch
2427

2428 2429 2430 2431
Traits give us a language for defining predicates on types, or
abstract properties that types can have. We can use this language to
define _bounds_ on type parameters, so that we can then operate on
generic types.
2432 2433

~~~~
2434
# trait Printable { fn print(&self); }
2435
fn print_all<T: Printable>(printable_things: Vec<T>) {
2436
    for thing in printable_things.iter() {
2437 2438
        thing.print();
    }
2439
}
2440
~~~~
2441

2442
Declaring `T` as conforming to the `Printable` trait (as we earlier
P
Patrick Walton 已提交
2443
did with `Clone`) makes it possible to call methods from that trait
2444
on values of type `T` inside the function. It will also cause a
2445
compile-time error when anyone tries to call `print_all` on a vector
2446 2447
whose element type does not have a `Printable` implementation.

2448
Type parameters can have multiple bounds by separating them with `+`,
2449
as in this version of `print_all` that copies elements.
2450 2451

~~~
2452
# trait Printable { fn print(&self); }
2453
fn print_all<T: Printable + Clone>(printable_things: Vec<T>) {
2454 2455
    let mut i = 0;
    while i < printable_things.len() {
N
Nick Cameron 已提交
2456
        let copy_of_thing = printable_things[i].clone();
2457
        copy_of_thing.print();
D
Damien Grassart 已提交
2458
        i += 1;
2459
    }
2460
}
2461
~~~
2462

2463 2464 2465
Method calls to bounded type parameters are _statically dispatched_,
imposing no more overhead than normal function invocation, so are
the preferred way to use traits polymorphically.
2466

2467 2468
This usage of traits is similar to Haskell type classes.

2469 2470 2471 2472 2473 2474 2475 2476
## Trait objects and dynamic method dispatch

The above allows us to define functions that polymorphically act on
values of a single unknown type that conforms to a given trait.
However, consider this function:

~~~~
# type Circle = int; type Rectangle = int;
2477
# impl Drawable for int { fn draw(&self) {} }
2478
# fn new_circle() -> int { 1 }
2479
trait Drawable { fn draw(&self); }
2480

2481
fn draw_all<T: Drawable>(shapes: Vec<T>) {
2482
    for shape in shapes.iter() { shape.draw(); }
2483 2484
}
# let c: Circle = new_circle();
2485
# draw_all(vec![c]);
2486 2487
~~~~

2488
You can call that on a vector of circles, or a vector of rectangles
2489
(assuming those have suitable `Drawable` traits defined), but not on
2490
a vector containing both circles and rectangles. When such behavior is
2491 2492 2493 2494
needed, a trait name can alternately be used as a type, called
an _object_.

~~~~
2495
# trait Drawable { fn draw(&self); }
2496
fn draw_all(shapes: &[Box<Drawable>]) {
2497
    for shape in shapes.iter() { shape.draw(); }
2498 2499 2500
}
~~~~

M
Michael Reinhard 已提交
2501
In this example, there is no type parameter. Instead, the `Box<Drawable>`
E
Eduard Burtescu 已提交
2502 2503 2504
type denotes any owned box value that implements the `Drawable` trait.
To construct such a value, you use the `as` operator to cast a value
to an object:
2505 2506 2507

~~~~
# type Circle = int; type Rectangle = bool;
2508
# trait Drawable { fn draw(&self); }
2509 2510
# fn new_circle() -> Circle { 1 }
# fn new_rectangle() -> Rectangle { true }
2511
# fn draw_all(shapes: &[Box<Drawable>]) {}
2512

2513 2514
impl Drawable for Circle { fn draw(&self) { /* ... */ } }
impl Drawable for Rectangle { fn draw(&self) { /* ... */ } }
2515

2516 2517 2518
let c: Box<Circle> = box new_circle();
let r: Box<Rectangle> = box new_rectangle();
draw_all([c as Box<Drawable>, r as Box<Drawable>]);
2519 2520 2521 2522 2523 2524 2525 2526
~~~~

We omit the code for `new_circle` and `new_rectangle`; imagine that
these just return `Circle`s and `Rectangle`s with a default size. Note
that, like strings and vectors, objects have dynamic size and may
only be referred to via one of the pointer types.
Other pointer types work as well.
Casts to traits may only be done with compatible pointers so,
2527
for example, an `&Circle` may not be cast to a `Box<Drawable>`.
2528 2529 2530

~~~
# type Circle = int; type Rectangle = int;
2531
# trait Drawable { fn draw(&self); }
2532
# impl Drawable for int { fn draw(&self) {} }
2533 2534 2535
# fn new_circle() -> int { 1 }
# fn new_rectangle() -> int { 2 }
// An owned object
2536
let owny: Box<Drawable> = box new_circle() as Box<Drawable>;
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
// A borrowed object
let stacky: &Drawable = &new_circle() as &Drawable;
~~~

Method calls to trait types are _dynamically dispatched_. Since the
compiler doesn't know specifically which functions to call at compile
time, it uses a lookup table (also known as a vtable or dictionary) to
select the method to call at runtime.

This usage of traits is similar to Java interfaces.

A
Alex Crichton 已提交
2548
There are some built-in bounds, such as `Send` and `Sync`, which are properties
2549 2550
of the components of types. By design, trait objects don't know the exact type
of their contents and so the compiler cannot reason about those properties.
2551

2552
You can instruct the compiler, however, that the contents of a trait object must
2553
ascribe to a particular bound with a trailing colon (`:`). These are examples of
2554
valid types:
2555

2556 2557 2558
~~~rust
trait Foo {}
trait Bar<T> {}
2559

A
Alex Crichton 已提交
2560
fn sendable_foo(f: Box<Foo + Send>) { /* ... */ }
A
Alex Crichton 已提交
2561
fn sync_bar<T: Sync>(b: &Bar<T> + Sync) { /* ... */ }
2562 2563
~~~

2564
When no colon is specified (such as the type `Box<Foo>`), it is inferred that the
2565 2566
value ascribes to no bounds. They must be added manually if any bounds are
necessary for usage.
2567 2568

Builtin kind bounds can also be specified on closure types in the same way (for
2569
example, by writing `fn:Send()`), and the default behaviours are the same as
2570 2571
for traits of the same storage class.

2572
## Trait inheritance
2573

2574 2575
We can write a trait declaration that _inherits_ from other traits, called _supertraits_.
Types that implement a trait must also implement its supertraits.
2576 2577
For example,
we can define a `Circle` trait that inherits from `Shape`.
2578 2579

~~~~
D
Daniel Micay 已提交
2580 2581
trait Shape { fn area(&self) -> f64; }
trait Circle : Shape { fn radius(&self) -> f64; }
2582 2583
~~~~

2584
Now, we can implement `Circle` on a type only if we also implement `Shape`.
2585 2586

~~~~
2587
use std::f64::consts::PI;
D
Daniel Micay 已提交
2588 2589 2590 2591 2592
# trait Shape { fn area(&self) -> f64; }
# trait Circle : Shape { fn radius(&self) -> f64; }
# struct Point { x: f64, y: f64 }
# fn square(x: f64) -> f64 { x * x }
struct CircleStruct { center: Point, radius: f64 }
2593
impl Circle for CircleStruct {
2594
    fn radius(&self) -> f64 { (self.area() / PI).sqrt() }
2595
}
2596
impl Shape for CircleStruct {
2597
    fn area(&self) -> f64 { PI * square(self.radius) }
2598
}
2599 2600
~~~~

2601 2602
Notice that methods of `Circle` can call methods on `Shape`, as our
`radius` implementation calls the `area` method.
2603
This is a silly way to compute the radius of a circle
P
Pavel Panchekha 已提交
2604
(since we could just return the `radius` field), but you get the idea.
2605

2606 2607
In type-parameterized functions,
methods of the supertrait may be called on values of subtrait-bound type parameters.
2608
Referring to the previous example of `trait Circle : Shape`:
2609 2610

~~~
D
Daniel Micay 已提交
2611 2612 2613
# trait Shape { fn area(&self) -> f64; }
# trait Circle : Shape { fn radius(&self) -> f64; }
fn radius_times_area<T: Circle>(c: T) -> f64 {
2614 2615 2616 2617 2618 2619 2620
    // `c` is both a Circle and a Shape
    c.radius() * c.area()
}
~~~

Likewise, supertrait methods may also be called on trait objects.

2621
~~~
2622
use std::f64::consts::PI;
D
Daniel Micay 已提交
2623 2624 2625 2626
# trait Shape { fn area(&self) -> f64; }
# trait Circle : Shape { fn radius(&self) -> f64; }
# struct Point { x: f64, y: f64 }
# struct CircleStruct { center: Point, radius: f64 }
2627 2628
# impl Circle for CircleStruct { fn radius(&self) -> f64 { (self.area() / PI).sqrt() } }
# impl Shape for CircleStruct { fn area(&self) -> f64 { PI * square(self.radius) } }
2629
# fn square(x: f64) -> f64 { x * x }
2630

2631 2632
let concrete = box CircleStruct{center:Point{x:3.0,y:4.0},radius:5.0};
let mycircle: Box<Circle> = concrete as Box<Circle>;
2633 2634 2635
let nonsense = mycircle.radius() * mycircle.area();
~~~

2636
> *Note:* Trait inheritance does not actually work with objects yet
2637

2638 2639
## Deriving implementations for traits

J
Jonas Hietala 已提交
2640
A small number of traits in can have implementations
2641 2642 2643
that can be automatically derived. These instances are specified by
placing the `deriving` attribute on a data type declaration. For
example, the following will mean that `Circle` has an implementation
2644
for `PartialEq` and can be used with the equality operators, and that a value
2645 2646 2647
of type `ABC` can be randomly generated and converted to a string:

~~~
2648
extern crate rand;
J
Jonas Hietala 已提交
2649
use std::rand::{task_rng, Rng};
2650

2651
#[deriving(PartialEq)]
D
Daniel Micay 已提交
2652
struct Circle { radius: f64 }
2653

2654
#[deriving(Rand, Show)]
2655
enum ABC { A, B, C }
2656 2657 2658 2659

fn main() {
    // Use the Show trait to print "A, B, C."
    println!("{}, {}, {}", A, B, C);
J
Jonas Hietala 已提交
2660 2661 2662 2663 2664 2665 2666

    let mut rng = task_rng();

    // Use the Rand trait to generate a random variants.
    for _ in range(0i, 10) {
        println!("{}", rng.gen::<ABC>());
    }
2667
}
2668 2669
~~~

2670 2671
The full list of derivable traits is `PartialEq`, `Eq`, `PartialOrd`,
`Ord`, `Encodable`, `Decodable`, `Clone`,
2672
`Hash`, `Rand`, `Default`, `Zero`, `FromPrimitive` and `Show`.
2673

M
Marvin Löbel 已提交
2674
# Crates and the module system
2675

M
Marvin Löbel 已提交
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
Rust's module system is very powerful, but because of that also somewhat complex.
Nevertheless, this section will try to explain every important aspect of it.

## Crates

In order to speak about the module system, we first need to define the medium it exists in:

Let's say you've written a program or a library, compiled it, and got the resulting binary.
In Rust, the content of all source code that the compiler directly had to compile in order to end up with
that binary is collectively called a 'crate'.

For example, for a simple hello world program your crate only consists of this code:

~~~~
2690
// `main.rs`
M
Marvin Löbel 已提交
2691
fn main() {
2692
    println!("Hello world!");
M
Marvin Löbel 已提交
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706
}
~~~~

A crate is also the unit of independent compilation in Rust: `rustc` always compiles a single crate at a time,
from which it produces either a library or an executable.

Note that merely using an already compiled library in your code does not make it part of your crate.

## The module hierarchy

For every crate, all the code in it is arranged in a hierarchy of modules starting with a single
root module. That root module is called the 'crate root'.

All modules in a crate below the crate root are declared with the `mod` keyword:
2707 2708

~~~~
M
Marvin Löbel 已提交
2709 2710
// This is the crate root

2711
mod farm {
M
Marvin Löbel 已提交
2712 2713
    // This is the body of module 'farm' declared in the crate root.

2714 2715
    fn chicken() { println!("cluck cluck"); }
    fn cow() { println!("mooo"); }
M
Marvin Löbel 已提交
2716 2717 2718 2719

    mod barn {
        // Body of module 'barn'

2720
        fn hay() { println!("..."); }
M
Marvin Löbel 已提交
2721
    }
2722
}
2723

2724
fn main() {
2725
    println!("Hello farm!");
2726 2727 2728
}
~~~~

M
Marvin Löbel 已提交
2729 2730 2731
As you can see, your module hierarchy is now three modules deep: There is the crate root, which contains your `main()`
function, and the module `farm`. The module `farm` also contains two functions and a third module `barn`,
which contains a function `hay`.
2732

M
Marvin Löbel 已提交
2733
## Paths and visibility
2734

M
Marvin Löbel 已提交
2735 2736 2737
We've now defined a nice module hierarchy. But how do we access the items in it from our `main` function?
One way to do it is to simply fully qualifying it:

2738
~~~~ {.ignore}
M
Marvin Löbel 已提交
2739
mod farm {
2740
    fn chicken() { println!("cluck cluck"); }
M
Marvin Löbel 已提交
2741
    // ...
2742 2743
}

M
Marvin Löbel 已提交
2744
fn main() {
2745
    println!("Hello chicken!");
M
Marvin Löbel 已提交
2746 2747 2748 2749 2750 2751 2752

    ::farm::chicken(); // Won't compile yet, see further down
}
~~~~

The `::farm::chicken` construct is what we call a 'path'.

2753 2754
Because it's starting with a `::`, it's also a 'global path', which qualifies
an item by its full path in the module hierarchy relative to the crate root.
M
Marvin Löbel 已提交
2755

2756 2757
If the path were to start with a regular identifier, like `farm::chicken`, it
would be a 'local path' instead. We'll get to them later.
M
Marvin Löbel 已提交
2758

2759 2760 2761
Now, if you actually tried to compile this code example, you'll notice that you
get a `function 'chicken' is private` error. That's because by default, items
(`fn`, `struct`, `static`, `mod`, ...) are private.
M
Marvin Löbel 已提交
2762

2763 2764
To make them visible outside their containing modules, you need to mark them
_public_ with `pub`:
M
Marvin Löbel 已提交
2765 2766 2767

~~~~
mod farm {
2768 2769
    pub fn chicken() { println!("cluck cluck"); }
    pub fn cow() { println!("mooo"); }
M
Marvin Löbel 已提交
2770 2771 2772 2773
    // ...
}

fn main() {
2774
    println!("Hello chicken!");
M
Marvin Löbel 已提交
2775 2776 2777
    ::farm::chicken(); // This compiles now
}
~~~~
2778 2779

Visibility restrictions in Rust exist only at module boundaries. This
2780 2781 2782
is quite different from most object-oriented languages that also
enforce restrictions on objects themselves. That's not to say that
Rust doesn't support encapsulation: both struct fields and methods can
2783
be private. But this encapsulation is at the module level, not the
M
Marvin Löbel 已提交
2784 2785
struct level.

2786 2787
Fields are _private_ by default, and can be made _public_ with
the `pub` keyword:
2788 2789

~~~
M
Marvin Löbel 已提交
2790
mod farm {
2791
# pub type Chicken = int;
2792
# struct Human(int);
2793
# impl Human { pub fn rest(&self) { } }
2794
# pub fn make_me_a_farm() -> Farm { Farm { chickens: vec![], farmer: Human(0) } }
2795
    pub struct Farm {
2796
        chickens: Vec<Chicken>,
2797
        pub farmer: Human
2798 2799 2800
    }

    impl Farm {
2801 2802
        fn feed_chickens(&self) { /* ... */ }
        pub fn add_chicken(&self, c: Chicken) { /* ... */ }
2803 2804 2805 2806 2807 2808 2809 2810
    }

    pub fn feed_animals(farm: &Farm) {
        farm.feed_chickens();
    }
}

fn main() {
M
Marvin Löbel 已提交
2811 2812 2813 2814 2815 2816
    let f = make_me_a_farm();
    f.add_chicken(make_me_a_chicken());
    farm::feed_animals(&f);
    f.farmer.rest();

    // This wouldn't compile because both are private:
2817 2818
    // `f.feed_chickens();`
    // `let chicken_counter = f.chickens.len();`
2819 2820
}
# fn make_me_a_farm() -> farm::Farm { farm::make_me_a_farm() }
2821
# fn make_me_a_chicken() -> farm::Chicken { 0 }
2822
~~~
2823

2824 2825
Exact details and specifications about visibility rules can be found in the Rust
manual.
2826

M
Marvin Löbel 已提交
2827
## Files and modules
2828

2829
One important aspect of Rust's module system is that source files and modules are not the same thing. You define a module hierarchy, populate it with all your definitions, define visibility, maybe put in a `fn main()`, and that's it.
2830

2831 2832 2833
The only file that's relevant when compiling is the one that contains the body
of your crate root, and it's only relevant because you have to pass that file
to `rustc` to compile your crate.
2834

2835 2836
In principle, that's all you need: You can write any Rust program as one giant source file that contains your
crate root and everything else in `mod ... { ... }` declarations.
M
Marvin Löbel 已提交
2837

2838 2839 2840 2841 2842 2843 2844
However, in practice you usually want to split up your code into multiple
source files to make it more manageable. Rust allows you to move the body of
any module into its own source file. If you declare a module without its body,
like `mod foo;`, the compiler will look for the files `foo.rs` and `foo/mod.rs`
inside some directory (usually the same as of the source file containing the
`mod foo;` declaration). If it finds either, it uses the content of that file
as the body of the module. If it finds both, that's a compile error.
2845

2846
To move the content of `mod farm` into its own file, you can write:
M
Marvin Löbel 已提交
2847 2848

~~~~ {.ignore}
2849 2850
// `main.rs` - contains body of the crate root
mod farm; // Compiler will look for `farm.rs` and `farm/mod.rs`
2851 2852

fn main() {
2853
    println!("Hello farm!");
M
Marvin Löbel 已提交
2854
    ::farm::cow();
2855
}
2856 2857
~~~~

M
Marvin Löbel 已提交
2858
~~~~
2859
// `farm.rs` - contains body of module 'farm' in the crate root
2860 2861
pub fn chicken() { println!("cluck cluck"); }
pub fn cow() { println!("mooo"); }
2862

M
Marvin Löbel 已提交
2863
pub mod barn {
2864
    pub fn hay() { println!("..."); }
M
Marvin Löbel 已提交
2865 2866 2867
}
# fn main() { }
~~~~
2868

2869
In short, `mod foo;` is just syntactic sugar for `mod foo { /* content of <...>/foo.rs or <...>/foo/mod.rs */ }`.
2870

2871 2872
This also means that having two or more identical `mod foo;` declarations
somewhere in your crate hierarchy is generally a bad idea,
2873
just like copy-and-paste-ing a module into multiple places is a bad idea.
M
Marvin Löbel 已提交
2874 2875
Both will result in duplicate and mutually incompatible definitions.

2876 2877 2878
When `rustc` resolves these module declarations, it starts by looking in the
parent directory of the file containing the `mod foo` declaration. For example,
given a file with the module body:
M
Marvin Löbel 已提交
2879 2880

~~~ {.ignore}
2881
// `src/main.rs`
M
Marvin Löbel 已提交
2882 2883 2884 2885 2886 2887 2888 2889 2890
mod plants;
mod animals {
    mod fish;
    mod mammals {
        mod humans;
    }
}
~~~

2891
The compiler will look for these files, in this order:
M
Marvin Löbel 已提交
2892

F
Florian Gilcher 已提交
2893
~~~text
M
Marvin Löbel 已提交
2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
src/plants.rs
src/plants/mod.rs

src/animals/fish.rs
src/animals/fish/mod.rs

src/animals/mammals/humans.rs
src/animals/mammals/humans/mod.rs
~~~

2904 2905 2906
Keep in mind that identical module hierarchies can still lead to different path
lookups depending on how and where you've moved a module body to its own file.
For example, if you move the `animals` module into its own file:
2907 2908

~~~ {.ignore}
2909
// `src/main.rs`
2910 2911 2912
mod plants;
mod animals;
~~~
2913

2914
~~~ {.ignore}
2915
// `src/animals.rs` or `src/animals/mod.rs`
2916 2917 2918 2919 2920
mod fish;
mod mammals {
    mod humans;
}
~~~
2921

2922
...then the source files of `mod animals`'s submodules can either be in the same directory as the animals source file or in a subdirectory of its directory. If the animals file is `src/animals.rs`, `rustc` will look for:
2923

F
Florian Gilcher 已提交
2924
~~~text
2925
src/animals.rs
2926 2927 2928 2929 2930
    src/fish.rs
    src/fish/mod.rs

    src/mammals/humans.rs
    src/mammals/humans/mod.rs
2931
~~~
2932 2933

If the animals file is `src/animals/mod.rs`, `rustc` will look for:
2934

F
Florian Gilcher 已提交
2935
~~~text
2936
src/animals/mod.rs
2937 2938 2939 2940 2941 2942 2943 2944
    src/animals/fish.rs
    src/animals/fish/mod.rs

    src/animals/mammals/humans.rs
    src/animals/mammals/humans/mod.rs

~~~

2945
These rules allow you to write small modules consisting of single source files which can live in the same directory as well as large modules which group submodule source files in subdirectories.
M
Marvin Löbel 已提交
2946

2947 2948 2949
If you need to override where `rustc` will look for the file containing a
module's source code, use the `path` compiler directive. For example, to load a
`classified` module from a different file:
M
Marvin Löbel 已提交
2950 2951

~~~ {.ignore}
2952 2953
#[path="../../area51/alien.rs"]
mod classified;
M
Marvin Löbel 已提交
2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967
~~~

## Importing names into the local scope

Always referring to definitions in other modules with their global
path gets old really fast, so Rust has a way to import
them into the local scope of your module: `use`-statements.

They work like this: At the beginning of any module body, `fn` body, or any other block
you can write a list of `use`-statements, consisting of the keyword `use` and a __global path__ to an item
without the `::` prefix. For example, this imports `cow` into the local scope:

~~~
use farm::cow;
2968
# mod farm { pub fn cow() { println!("I'm a hidden ninja cow!") } }
M
Marvin Löbel 已提交
2969 2970 2971 2972 2973
# fn main() { cow() }
~~~

The path you give to `use` is per default global, meaning relative to the crate root,
no matter how deep the module hierarchy is, or whether the module body it's written in
2974
is contained in its own file. (Remember: files are irrelevant.)
M
Marvin Löbel 已提交
2975

2976
This is different from other languages, where you often only find a single import construct that combines the semantic
M
Marvin Löbel 已提交
2977
of `mod foo;` and `use`-statements, and which tend to work relative to the source file or use an absolute file path
2978
- Ruby's `require` or C/C++'s `#include` come to mind.
M
Marvin Löbel 已提交
2979 2980 2981 2982 2983 2984 2985

However, it's also possible to import things relative to the module of the `use`-statement:
Adding a `super::` in front of the path will start in the parent module,
while adding a `self::` prefix will start in the current module:

~~~
# mod workaround {
2986
# pub fn some_parent_item(){ println!("...") }
M
Marvin Löbel 已提交
2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004
# mod foo {
use super::some_parent_item;
use self::some_child_module::some_item;
# pub fn bar() { some_parent_item(); some_item() }
# pub mod some_child_module { pub fn some_item() {} }
# }
# }
~~~

Again - relative to the module, not to the file.

Imports are also shadowed by local definitions:
For each name you mention in a module/block, `rust`
will first look at all items that are defined locally,
and only if that results in no match look at items you brought in
scope with corresponding `use` statements.

~~~ {.ignore}
3005
# // FIXME: Allow unused import in doc test
M
Marvin Löbel 已提交
3006 3007
use farm::cow;
// ...
3008 3009
# mod farm { pub fn cow() { println!("Hidden ninja cow is hidden.") } }
fn cow() { println!("Mooo!") }
M
Marvin Löbel 已提交
3010 3011

fn main() {
3012
    cow() // resolves to the locally defined `cow()` function
3013
}
M
Marvin Löbel 已提交
3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026
~~~

To make this behavior more obvious, the rule has been made that `use`-statement always need to be written
before any declaration, like in the example above. This is a purely artificial rule introduced
because people always assumed they shadowed each other based on order, despite the fact that all items in rust are
mutually recursive, order independent definitions.

One odd consequence of that rule is that `use` statements also go in front of any `mod` declaration,
even if they refer to things inside them:

~~~
use farm::cow;
mod farm {
3027
    pub fn cow() { println!("Moooooo?") }
M
Marvin Löbel 已提交
3028 3029 3030 3031 3032 3033 3034
}

fn main() { cow() }
~~~

This is what our `farm` example looks like with `use` statements:

3035
~~~~
M
Marvin Löbel 已提交
3036 3037 3038
use farm::chicken;
use farm::cow;
use farm::barn;
3039

M
Marvin Löbel 已提交
3040
mod farm {
3041 3042
    pub fn chicken() { println!("cluck cluck"); }
    pub fn cow() { println!("mooo"); }
3043

M
Marvin Löbel 已提交
3044
    pub mod barn {
3045
        pub fn hay() { println!("..."); }
M
Marvin Löbel 已提交
3046 3047
    }
}
3048

M
Marvin Löbel 已提交
3049
fn main() {
3050
    println!("Hello farm!");
3051

M
Marvin Löbel 已提交
3052 3053 3054 3055 3056 3057
    // Can now refer to those names directly:
    chicken();
    cow();
    barn::hay();
}
~~~~
3058

M
Marvin Löbel 已提交
3059
And here an example with multiple files:
N
Noufal Ibrahim 已提交
3060

M
Marvin Löbel 已提交
3061
~~~{.ignore}
3062
// `a.rs` - crate root
M
Marvin Löbel 已提交
3063
use b::foo;
3064
use b::c::bar;
M
Marvin Löbel 已提交
3065
mod b;
3066 3067 3068 3069
fn main() {
    foo();
    bar();
}
M
Marvin Löbel 已提交
3070
~~~
N
Noufal Ibrahim 已提交
3071

M
Marvin Löbel 已提交
3072
~~~{.ignore}
3073
// `b/mod.rs`
M
Marvin Löbel 已提交
3074
pub mod c;
D
dgoon 已提交
3075
pub fn foo() { println!("Foo!"); }
M
Marvin Löbel 已提交
3076
~~~
N
Noufal Ibrahim 已提交
3077

3078 3079 3080
~~~{.ignore}
// `b/c.rs`
pub fn bar() { println!("Bar!"); }
M
Marvin Löbel 已提交
3081
~~~
3082

M
Marvin Löbel 已提交
3083
There also exist two short forms for importing multiple names at once:
3084

M
Marvin Löbel 已提交
3085
1. Explicit mention multiple names as the last element of an `use` path:
3086

M
Marvin Löbel 已提交
3087 3088 3089
~~~
use farm::{chicken, cow};
# mod farm {
3090 3091
#     pub fn cow() { println!("Did I already mention how hidden and ninja I am?") }
#     pub fn chicken() { println!("I'm Bat-chicken, guardian of the hidden tutorial code.") }
M
Marvin Löbel 已提交
3092 3093 3094 3095 3096
# }
# fn main() { cow(); chicken() }
~~~

2. Import everything in a module with a wildcard:
3097

M
Marvin Löbel 已提交
3098
~~~
3099
# #![feature(globs)]
M
Marvin Löbel 已提交
3100 3101
use farm::*;
# mod farm {
3102 3103
#     pub fn cow() { println!("Bat-chicken? What a stupid name!") }
#     pub fn chicken() { println!("Says the 'hidden ninja' cow.") }
M
Marvin Löbel 已提交
3104 3105 3106 3107
# }
# fn main() { cow(); chicken() }
~~~

3108
> *Note:* This feature of the compiler is currently gated behind the
3109
> `#![feature(globs)]` directive. More about these directives can be found in
3110 3111
> the manual.

M
Marvin Löbel 已提交
3112 3113 3114
However, that's not all. You can also rename an item while you're bringing it into scope:

~~~
3115
use farm::chicken as egg_layer;
3116
# mod farm { pub fn chicken() { println!("Laying eggs is fun!")  } }
M
Marvin Löbel 已提交
3117 3118 3119 3120 3121 3122 3123
// ...

fn main() {
    egg_layer();
}
~~~

J
Jorge Aparicio 已提交
3124
In general, `use` creates a local alias:
M
Marvin Löbel 已提交
3125
An alternate path and a possibly different name to access the same item,
3126
without touching the original, and with both being interchangeable.
M
Marvin Löbel 已提交
3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137

## Reexporting names

It is also possible to reexport items to be accessible under your module.

For that, you write `pub use`:

~~~
mod farm {
    pub use self::barn::hay;

3138 3139
    pub fn chicken() { println!("cluck cluck"); }
    pub fn cow() { println!("mooo"); }
M
Marvin Löbel 已提交
3140 3141

    mod barn {
3142
        pub fn hay() { println!("..."); }
M
Marvin Löbel 已提交
3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160
    }
}

fn main() {
    farm::chicken();
    farm::cow();
    farm::hay();
}
~~~

Just like in normal `use` statements, the exported names
merely represent an alias to the same thing and can also be renamed.

The above example also demonstrate what you can use `pub use` for:
The nested `barn` module is private, but the `pub use` allows users
of the module `farm` to access a function from `barn` without needing
to know that `barn` exists.

3161
In other words, you can use it to decouple a public api from its internal implementation.
M
Marvin Löbel 已提交
3162 3163 3164 3165 3166 3167 3168 3169 3170 3171

## Using libraries

So far we've only talked about how to define and structure your own crate.

However, most code out there will want to use preexisting libraries,
as there really is no reason to start from scratch each time you start a new project.

In Rust terminology, we need a way to refer to other crates.

A
Alex Crichton 已提交
3172
For that, Rust offers you the `extern crate` declaration:
M
Marvin Löbel 已提交
3173 3174

~~~
J
Jonas Hietala 已提交
3175
// `num` ships with Rust.
A
Alex Crichton 已提交
3176
extern crate num;
M
Marvin Löbel 已提交
3177 3178 3179

fn main() {
    // The rational number '1/2':
3180
    let one_half = ::num::rational::Ratio::new(1i, 2);
M
Marvin Löbel 已提交
3181 3182 3183
}
~~~

A
Alex Crichton 已提交
3184
A statement of the form `extern crate foo;` will cause `rustc` to search for the crate `foo`,
M
Marvin Löbel 已提交
3185 3186 3187 3188 3189 3190 3191
and if it finds a matching binary it lets you use it from inside your crate.

The effect it has on your module hierarchy mirrors aspects of both `mod` and `use`:

- Like `mod`, it causes `rustc` to actually emit code:
  The linkage information the binary needs to use the library `foo`.

A
Alex Crichton 已提交
3192
- But like `use`, all `extern crate` statements that refer to the same library are interchangeable,
B
Ben Striegel 已提交
3193 3194
  as each one really just presents an alias to an external module (the crate root of the library
  you're linking against).
M
Marvin Löbel 已提交
3195 3196

Remember how `use`-statements have to go before local declarations because the latter shadows the former?
A
Alex Crichton 已提交
3197 3198
Well, `extern crate` statements also have their own rules in that regard:
Both `use` and local declarations can shadow them, so the rule is that `extern crate` has to go in front
M
Marvin Löbel 已提交
3199 3200 3201 3202 3203
of both `use` and local declarations.

Which can result in something like this:

~~~
A
Alex Crichton 已提交
3204
extern crate num;
M
Marvin Löbel 已提交
3205 3206

use farm::dog;
3207
use num::rational::Ratio;
M
Marvin Löbel 已提交
3208 3209

mod farm {
3210
    pub fn dog() { println!("woof"); }
M
Marvin Löbel 已提交
3211 3212 3213 3214
}

fn main() {
    farm::dog();
3215
    let a_third = Ratio::new(1i, 3);
M
Marvin Löbel 已提交
3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227
}
~~~

It's a bit weird, but it's the result of shadowing rules that have been set that way because
they model most closely what people expect to shadow.

## Crate metadata and settings

For every crate you can define a number of metadata items, such as link name, version or author.
You can also toggle settings that have crate-global consequences. Both mechanism
work by providing attributes in the crate root.

E
Erik Lyon 已提交
3228
For example, Rust uniquely identifies crates by their link metadata, which includes
M
Marvin Löbel 已提交
3229 3230 3231 3232
the link name and the version. It also hashes the filename and the symbols in a binary
based on the link metadata, allowing you to use two different versions of the same library in a crate
without conflict.

3233
Therefore, if you plan to compile your crate as a library, you should annotate it with that information:
3234

3235
~~~~
3236
# #![allow(unused_attribute)]
3237
// `lib.rs`
M
Marvin Löbel 已提交
3238

3239 3240
# #![crate_type = "lib"]
#![crate_id = "farm#2.5"]
M
Marvin Löbel 已提交
3241 3242

// ...
A
Alex Crichton 已提交
3243
# fn farm() {}
3244 3245
~~~~

3246
You can also specify crate id information in a `extern crate` statement.  For
A
Alex Crichton 已提交
3247
example, these `extern crate` statements would both accept and select the
3248
crate define above:
3249

3250
~~~~ {.ignore}
A
Alex Crichton 已提交
3251 3252 3253
extern crate farm;
extern crate farm = "farm#2.5";
extern crate my_farm = "farm";
M
Marvin Löbel 已提交
3254 3255 3256 3257 3258
~~~~

Other crate settings and metadata include things like enabling/disabling certain errors or warnings,
or setting the crate type (library or executable) explicitly:

3259
~~~~
3260
# #![allow(unused_attribute)]
3261
// `lib.rs`
M
Marvin Löbel 已提交
3262
// ...
3263

M
Marvin Löbel 已提交
3264
// This crate is a library ("bin" is the default)
3265 3266
#![crate_id = "farm#2.5"]
#![crate_type = "lib"]
M
Marvin Löbel 已提交
3267 3268 3269

// Turn on a warning
#[warn(non_camel_case_types)]
A
Alex Crichton 已提交
3270
# fn farm() {}
M
Marvin Löbel 已提交
3271 3272
~~~~

3273 3274
## A minimal example

M
Marvin Löbel 已提交
3275 3276 3277
Now for something that you can actually compile yourself.

We define two crates, and use one of them as a library in the other.
3278

3279
~~~~
3280
# #![allow(unused_attribute)]
3281
// `world.rs`
3282
#![crate_id = "world#0.42"]
3283 3284

# mod secret_module_to_make_this_test_run {
M
Marvin Löbel 已提交
3285
pub fn explore() -> &'static str { "world" }
3286
# }
3287 3288
~~~~

3289
~~~~ {.ignore}
3290
// `main.rs`
A
Alex Crichton 已提交
3291
extern crate world;
3292
fn main() { println!("hello {}", world::explore()); }
3293 3294 3295 3296
~~~~

Now compile and run like this (adjust to your platform if necessary):

F
Florian Gilcher 已提交
3297
~~~~console
3298
$ rustc --crate-type=lib world.rs  # compiles libworld-<HASH>-0.42.rlib
F
Florian Gilcher 已提交
3299 3300
$ rustc main.rs -L .               # compiles main
$ ./main
3301 3302 3303
"hello world"
~~~~

M
Marvin Löbel 已提交
3304 3305 3306
Notice that the library produced contains the version in the file name
as well as an inscrutable string of alphanumerics. As explained in the previous paragraph,
these are both part of Rust's library versioning scheme. The alphanumerics are
3307
a hash representing the crate's id.
M
Marvin Löbel 已提交
3308 3309 3310 3311

## The standard library and the prelude

While reading the examples in this tutorial, you might have asked yourself where all
3312
those magical predefined items like `range` are coming from.
M
Marvin Löbel 已提交
3313 3314 3315 3316 3317 3318 3319

The truth is, there's nothing magical about them: They are all defined normally
in the `std` library, which is a crate that ships with Rust.

The only magical thing that happens is that `rustc` automatically inserts this line into your crate root:

~~~ {.ignore}
A
Alex Crichton 已提交
3320
extern crate std;
M
Marvin Löbel 已提交
3321 3322 3323 3324 3325 3326 3327 3328
~~~

As well as this line into every module body:

~~~ {.ignore}
use std::prelude::*;
~~~

E
Erik Lyon 已提交
3329
The role of the `prelude` module is to re-export common definitions from `std`.
M
Marvin Löbel 已提交
3330

3331
This allows you to use common types and functions like `Option<T>` or `range`
M
Marvin Löbel 已提交
3332 3333 3334
without needing to import them. And if you need something from `std` that's not in the prelude,
you just have to import it with an `use` statement.

3335
For example, it re-exports `range` which is defined in `std::iter::range`:
M
Marvin Löbel 已提交
3336 3337

~~~
3338
use std::iter::range as iter_range;
M
Marvin Löbel 已提交
3339 3340

fn main() {
3341
    // `range` is imported by default
3342
    for _ in range(0u, 10) {}
3343 3344

    // Doesn't hinder you from importing it under a different name yourself
3345
    for _ in iter_range(0u, 10) {}
3346 3347

    // Or from not using the automatic import.
3348
    for _ in ::std::iter::range(0u, 10) {}
M
Marvin Löbel 已提交
3349 3350 3351 3352 3353 3354
}
~~~

Both auto-insertions can be disabled with an attribute if necessary:

~~~
3355
# #![allow(unused_attribute)]
M
Marvin Löbel 已提交
3356
// In the crate root:
3357
#![no_std]
M
Marvin Löbel 已提交
3358 3359 3360
~~~

~~~
3361
# #![allow(unused_attribute)]
M
Marvin Löbel 已提交
3362
// In any module:
3363
#![no_implicit_prelude]
M
Marvin Löbel 已提交
3364
~~~
3365

3366 3367 3368
See the [API documentation][stddoc] for details.

[stddoc]: std/index.html
3369

3370
# What next?
3371

3372
Now that you know the essentials, check out any of the additional
3373
guides on individual topics.
3374

3375 3376
* [Pointers][pointers]
* [Lifetimes][lifetimes]
3377 3378 3379
* [Tasks and communication][tasks]
* [Macros][macros]
* [The foreign function interface][ffi]
3380
* [Containers and iterators][container]
C
Corey Richardson 已提交
3381
* [Documenting Rust code][rustdoc]
J
Julia Evans 已提交
3382
* [Testing Rust code][testing]
3383
* [The Rust Runtime][runtime]
3384

M
Michael Zhou 已提交
3385
There is further documentation on the [wiki], however those tend to be even more out of date than this document.
3386

3387 3388
[pointers]: guide-pointers.html
[lifetimes]: guide-lifetimes.html
3389 3390 3391 3392 3393
[tasks]: guide-tasks.html
[macros]: guide-macros.html
[ffi]: guide-ffi.html
[container]: guide-container.html
[testing]: guide-testing.html
3394
[runtime]: guide-runtime.html
C
Corey Richardson 已提交
3395
[rustdoc]: rustdoc.html
B
Brian Anderson 已提交
3396
[wiki]: https://github.com/rust-lang/rust/wiki/Docs
3397

B
Brian Anderson 已提交
3398
[wiki-packages]: https://github.com/rust-lang/rust/wiki/Doc-packages,-editors,-and-other-tools