]> git.lizzy.rs Git - rust.git/blob - src/doc/tutorial.md
da78869d9fa84a69856a8272d151865537a861a7
[rust.git] / src / doc / tutorial.md
1 % The Rust Language Tutorial
2
3 # Introduction
4
5 Rust is a programming language with a focus on type safety, memory
6 safety, concurrency and performance. It is intended for writing
7 large-scale, high-performance software that is free from several
8 classes of common errors. Rust has a sophisticated memory model that
9 encourages efficient data structures and safe concurrency patterns,
10 forbidding invalid memory accesses that would otherwise cause
11 segmentation faults. It is statically typed and compiled ahead of
12 time.
13
14 As a multi-paradigm language, Rust supports writing code in
15 procedural, functional and object-oriented styles. Some of its
16 pleasant high-level features include:
17
18 * **Type inference.** Type annotations on local variable declarations
19   are optional.
20 * **Safe task-based concurrency.** Rust's lightweight tasks do not share
21   memory, instead communicating through messages.
22 * **Higher-order functions.** Efficient and flexible closures provide
23   iteration and other control structures
24 * **Pattern matching and algebraic data types.** Pattern matching on
25   Rust's enumeration types (a more powerful version of C's enums,
26   similar to algebraic data types in functional languages) is a
27   compact and expressive way to encode program logic.
28 * **Polymorphism.** Rust has type-parametric functions and
29   types, type classes and OO-style interfaces.
30
31 ## Scope
32
33 This is an introductory tutorial for the Rust programming language. It
34 covers the fundamentals of the language, including the syntax, the
35 type system and memory model, generics, and modules. [Additional
36 tutorials](#what-next?) cover specific language features in greater
37 depth.
38
39 This tutorial assumes that the reader is already familiar with one or
40 more languages in the C family. Understanding of pointers and general
41 memory management techniques will help.
42
43 ## Conventions
44
45 Throughout the tutorial, language keywords and identifiers defined in
46 example code are displayed in `code font`.
47
48 Code snippets are indented, and also shown in a monospaced font. Not
49 all snippets constitute whole programs. For brevity, we'll often show
50 fragments of programs that don't compile on their own. To try them
51 out, you might have to wrap them in `fn main() { ... }`, and make sure
52 they don't contain references to names that aren't actually defined.
53
54 > *Warning:* Rust is a language under ongoing development. Notes
55 > about potential changes to the language, implementation
56 > deficiencies, and other caveats appear offset in blockquotes.
57
58 # Getting started
59
60 > *Warning:* The tarball and installer links are for the most recent
61 > release, not master. To use master, you **must** build from [git].
62
63 The Rust compiler currently must be built from a [tarball] or [git], unless
64 you are on Windows, in which case using the [installer][win-exe] is
65 recommended. There is a list of community-maintained nightly builds and
66 packages [on the wiki][wiki-packages].
67
68 Since the Rust compiler is written in Rust, it must be built by
69 a precompiled "snapshot" version of itself (made in an earlier state
70 of development). The source build automatically fetches these snapshots
71 from the Internet on our supported platforms.
72
73 Snapshot binaries are currently built and tested on several platforms:
74
75 * Windows (7, 8, Server 2008 R2), x86 only
76 * Linux (2.6.18 or later, various distributions), x86 and x86-64
77 * OSX 10.7 (Lion) or greater, x86 and x86-64
78
79 You may find that other platforms work, but these are our "tier 1"
80 supported build environments that are most likely to work.
81
82 > *Note:* Windows users should read the detailed
83 > [Getting started][wiki-start] notes on the wiki. Even when using
84 > the binary installer, the Windows build requires a MinGW installation,
85 > the precise details of which are not discussed here.
86
87 [wiki-start]: https://github.com/mozilla/rust/wiki/Note-getting-started-developing-Rust
88 [git]: https://github.com/mozilla/rust.git
89
90 To build from source you will also need the following prerequisite
91 packages:
92
93 * g++ 4.7 or clang++ 3.x
94 * python 2.6 or later (but not 3.x)
95 * perl 5.0 or later
96 * gnu make 3.81 or later
97 * curl
98
99 If you've fulfilled those prerequisites, something along these lines
100 should work.
101
102 ~~~~ {.notrust}
103 $ curl -O http://static.rust-lang.org/dist/rust-nightly.tar.gz
104 $ tar -xzf rust-nightly.tar.gz
105 $ cd rust-nightly
106 $ ./configure
107 $ make && make install
108 ~~~~
109
110 You may need to use `sudo make install` if you do not normally have
111 permission to modify the destination directory. The install locations
112 can be adjusted by passing a `--prefix` argument to
113 `configure`. Various other options are also supported: pass `--help`
114 for more information on them.
115
116 When complete, `make install` will place several programs into
117 `/usr/local/bin`: `rustc`, the Rust compiler, and `rustdoc`, the
118 API-documentation tool.
119
120 [tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz
121 [win-exe]: http://static.rust-lang.org/dist/rust-nightly-install.exe
122
123 ## Compiling your first program
124
125 Rust program files are, by convention, given the extension `.rs`. Say
126 we have a file `hello.rs` containing this program:
127
128 ~~~~
129 fn main() {
130     println!("hello?");
131 }
132 ~~~~
133 > *Note:* An identifier followed by an exclamation point, like
134 > `println!`, is a macro invocation.  Macros are explained
135 > [later](#syntax-extensions); for now just remember to include the
136 > exclamation point.
137
138 If the Rust compiler was installed successfully, running `rustc
139 hello.rs` will produce an executable called `hello` (or `hello.exe` on
140 Windows) which, upon running, will likely do exactly what you expect.
141
142 The Rust compiler tries to provide useful information when it encounters an
143 error. If you introduce an error into the program (for example, by changing
144 `println!` to some nonexistent macro), and then compile it, you'll see
145 an error message like this:
146
147 ~~~~ {.notrust}
148 hello.rs:2:5: 2:24 error: macro undefined: 'print_with_unicorns'
149 hello.rs:2     print_with_unicorns!("hello?");
150                ^~~~~~~~~~~~~~~~~~~
151 ~~~~
152
153 In its simplest form, a Rust program is a `.rs` file with some types
154 and functions defined in it. If it has a `main` function, it can be
155 compiled to an executable. Rust does not allow code that's not a
156 declaration to appear at the top level of the file: all statements must
157 live inside a function.  Rust programs can also be compiled as
158 libraries, and included in other programs, even ones not written in Rust.
159
160 ## Editing Rust code
161
162 There are vim highlighting and indentation scripts in the Rust source
163 distribution under `src/etc/vim/`. There is an emacs mode under
164 `src/etc/emacs/` called `rust-mode`, but do read the instructions
165 included in that directory. In particular, if you are running emacs
166 24, then using emacs's internal package manager to install `rust-mode`
167 is the easiest way to keep it up to date. There is also a package for
168 Sublime Text 2, available both [standalone][sublime] and through
169 [Sublime Package Control][sublime-pkg], and support for Kate
170 under `src/etc/kate`.
171
172 A community-maintained list of available Rust tooling is [on the
173 wiki][wiki-packages].
174
175 There is ctags support via `src/etc/ctags.rust`, but many other
176 tools and editors are not yet supported. If you end up writing a Rust
177 mode for your favorite editor, let us know so that we can link to it.
178
179 [sublime]: http://github.com/dbp/sublime-rust
180 [sublime-pkg]: http://wbond.net/sublime_packages/package_control
181
182 # Syntax basics
183
184 Assuming you've programmed in any C-family language (C++, Java,
185 JavaScript, C#, or PHP), Rust will feel familiar. Code is arranged
186 in blocks delineated by curly braces; there are control structures
187 for branching and looping, like the familiar `if` and `while`; function
188 calls are written `myfunc(arg1, arg2)`; operators are written the same
189 and mostly have the same precedence as in C; comments are again like C;
190 module names are separated with double-colon (`::`) as with C++.
191
192 The main surface difference to be aware of is that the condition at
193 the head of control structures like `if` and `while` does not require
194 parentheses, while their bodies *must* be wrapped in
195 braces. Single-statement, unbraced bodies are not allowed.
196
197 ~~~~
198 # mod universe { pub fn recalibrate() -> bool { true } }
199 fn main() {
200     /* A simple loop */
201     loop {
202         // A tricky calculation
203         if universe::recalibrate() {
204             return;
205         }
206     }
207 }
208 ~~~~
209
210 The `let` keyword introduces a local variable. Variables are immutable by
211 default. To introduce a local variable that you can re-assign later, use `let
212 mut` instead.
213
214 ~~~~
215 let hi = "hi";
216 let mut count = 0;
217
218 while count < 10 {
219     println!("count is {}", count);
220     count += 1;
221 }
222 ~~~~
223
224 Although Rust can almost always infer the types of local variables, you can
225 specify a variable's type by following it in the `let` with a colon, then the
226 type name. Static items, on the other hand, always require a type annotation.
227
228
229 ~~~~
230 static MONSTER_FACTOR: f64 = 57.8;
231 let monster_size = MONSTER_FACTOR * 10.0;
232 let monster_size: int = 50;
233 ~~~~
234
235 Local variables may shadow earlier declarations, as in the previous example:
236 `monster_size` was first declared as a `f64`, and then a second
237 `monster_size` was declared as an `int`. If you were to actually compile this
238 example, though, the compiler would determine that the first `monster_size` is
239 unused and issue a warning (because this situation is likely to indicate a
240 programmer error). For occasions where unused variables are intentional, their
241 names may be prefixed with an underscore to silence the warning, like `let
242 _monster_size = 50;`.
243
244 Rust identifiers start with an alphabetic
245 character or an underscore, and after that may contain any sequence of
246 alphabetic characters, numbers, or underscores. The preferred style is to
247 write function, variable, and module names with lowercase letters, using
248 underscores where they help readability, while writing types in camel case.
249
250 ~~~
251 let my_variable = 100;
252 type MyType = int;     // primitive types are _not_ camel case
253 ~~~
254
255 ## Expressions and semicolons
256
257 Though it isn't apparent in all code, there is a fundamental
258 difference between Rust's syntax and predecessors like C.
259 Many constructs that are statements in C are expressions
260 in Rust, allowing code to be more concise. For example, you might
261 write a piece of code like this:
262
263 ~~~~
264 # let item = "salad";
265 let price;
266 if item == "salad" {
267     price = 3.50;
268 } else if item == "muffin" {
269     price = 2.25;
270 } else {
271     price = 2.00;
272 }
273 ~~~~
274
275 But, in Rust, you don't have to repeat the name `price`:
276
277 ~~~~
278 # let item = "salad";
279 let price =
280     if item == "salad" {
281         3.50
282     } else if item == "muffin" {
283         2.25
284     } else {
285         2.00
286     };
287 ~~~~
288
289 Both pieces of code are exactly equivalent: they assign a value to
290 `price` depending on the condition that holds. Note that there
291 are no semicolons in the blocks of the second snippet. This is
292 important: the lack of a semicolon after the last statement in a
293 braced block gives the whole block the value of that last expression.
294
295 Put another way, the semicolon in Rust *ignores the value of an expression*.
296 Thus, if the branches of the `if` had looked like `{ 4; }`, the above example
297 would simply assign `()` (unit or void) to `price`. But without the semicolon, each
298 branch has a different value, and `price` gets the value of the branch that
299 was taken.
300
301 In short, everything that's not a declaration (declarations are `let` for
302 variables; `fn` for functions; and any top-level named items such as
303 [traits](#traits), [enum types](#enums), and static items) is an
304 expression, including function bodies.
305
306 ~~~~
307 fn is_four(x: int) -> bool {
308    // No need for a return statement. The result of the expression
309    // is used as the return value.
310    x == 4
311 }
312 ~~~~
313
314 ## Primitive types and literals
315
316 There are general signed and unsigned integer types, `int` and `uint`,
317 as well as 8-, 16-, 32-, and 64-bit variants, `i8`, `u16`, etc.
318 Integers can be written in decimal (`144`), hexadecimal (`0x90`), octal (`0o70`), or
319 binary (`0b10010000`) base. Each integral type has a corresponding literal
320 suffix that can be used to indicate the type of a literal: `i` for `int`,
321 `u` for `uint`, `i8` for the `i8` type.
322
323 In the absence of an integer literal suffix, Rust will infer the
324 integer type based on type annotations and function signatures in the
325 surrounding program. In the absence of any type information at all,
326 Rust will assume that an unsuffixed integer literal has type
327 `int`.
328
329 ~~~~
330 let a = 1;       // `a` is an `int`
331 let b = 10i;     // `b` is an `int`, due to the `i` suffix
332 let c = 100u;    // `c` is a `uint`
333 let d = 1000i32; // `d` is an `i32`
334 ~~~~
335
336 There are two floating-point types: `f32`, and `f64`.
337 Floating-point numbers are written `0.0`, `1e6`, or `2.1e-4`.
338 Like integers, floating-point literals are inferred to the correct type.
339 Suffixes `f32`, and `f64` can be used to create literals of a specific type.
340
341 The keywords `true` and `false` produce literals of type `bool`.
342
343 Characters, the `char` type, are four-byte Unicode codepoints,
344 whose literals are written between single quotes, as in `'x'`.
345 Just like C, Rust understands a number of character escapes, using the backslash
346 character, such as `\n`, `\r`, and `\t`. String literals,
347 written between double quotes, allow the same escape sequences, and do no
348 other processing, unlike languages such as PHP or shell.
349
350 On the other hand, raw string literals do not process any escape sequences.
351 They are written as `r##"blah"##`, with a matching number of zero or more `#`
352 before the opening and after the closing quote, and can contain any sequence of
353 characters except their closing delimiter.  More on strings
354 [later](#vectors-and-strings).
355
356 The unit type, written `()`, has a single value, also written `()`.
357
358 ## Operators
359
360 Rust's set of operators contains very few surprises. Arithmetic is done with
361 `*`, `/`, `%`, `+`, and `-` (multiply, quotient, remainder, add, and subtract). `-` is
362 also a unary prefix operator that negates numbers. As in C, the bitwise operators
363 `>>`, `<<`, `&`, `|`, and `^` are also supported.
364
365 Note that, if applied to an integer value, `!` flips all the bits (bitwise
366 NOT, like `~` in C).
367
368 The comparison operators are the traditional `==`, `!=`, `<`, `>`,
369 `<=`, and `>=`. Short-circuiting (lazy) boolean operators are written
370 `&&` (and) and `||` (or).
371
372 For compile-time type casting, Rust uses the binary `as` operator.  It takes
373 an expression on the left side and a type on the right side and will, if a
374 meaningful conversion exists, convert the result of the expression to the
375 given type. Generally, `as` is only used with the primitive numeric types or
376 pointers, and is not overloadable.  [`transmute`][transmute] can be used for
377 unsafe C-like casting of same-sized types.
378
379 ~~~~
380 let x: f64 = 4.0;
381 let y: uint = x as uint;
382 assert!(y == 4u);
383 ~~~~
384
385 [transmute]: http://doc.rust-lang.org/std/mem/fn.transmute.html
386
387 ## Syntax extensions
388
389 *Syntax extensions* are special forms that are not built into the language,
390 but are instead provided by the libraries. To make it clear to the reader when
391 a name refers to a syntax extension, the names of all syntax extensions end
392 with `!`. The standard library defines a few syntax extensions, the most
393 useful of which is [`format!`][fmt], a `sprintf`-like text formatter that you
394 will often see in examples, and its related family of macros: `print!`,
395 `println!`, and `write!`.
396
397 `format!` draws syntax from Python, but contains many of the same principles
398 that [printf][pf] has. Unlike printf, `format!` will give you a compile-time
399 error when the types of the directives don't match the types of the arguments.
400
401 ~~~~
402 # let mystery_object = ();
403
404 // `{}` will print the "default format" of a type
405 println!("{} is {}", "the answer", 43);
406
407 // `{:?}` will conveniently print any type
408 println!("what is this thing: {:?}", mystery_object);
409 ~~~~
410
411 [pf]: http://en.cppreference.com/w/cpp/io/c/fprintf
412 [fmt]: http://doc.rust-lang.org/std/fmt/
413
414 You can define your own syntax extensions with the macro system. For details,
415 see the [macro tutorial][macros]. Note that macro definition is currently
416 considered an unstable feature.
417
418 # Control structures
419
420 ## Conditionals
421
422 We've seen `if` expressions a few times already. To recap, braces are
423 compulsory, an `if` can have an optional `else` clause, and multiple
424 `if`/`else` constructs can be chained together:
425
426 ~~~~
427 if false {
428     println!("that's odd");
429 } else if true {
430     println!("right");
431 } else {
432     println!("neither true nor false");
433 }
434 ~~~~
435
436 The condition given to an `if` construct *must* be of type `bool` (no
437 implicit conversion happens). If the arms are blocks that have a
438 value, this value must be of the same type for every arm in which
439 control reaches the end of the block:
440
441 ~~~~
442 fn signum(x: int) -> int {
443     if x < 0 { -1 }
444     else if x > 0 { 1 }
445     else { 0 }
446 }
447 ~~~~
448
449 ## Pattern matching
450
451 Rust's `match` construct is a generalized, cleaned-up version of C's
452 `switch` construct. You provide it with a value and a number of
453 *arms*, each labelled with a pattern, and the code compares the value
454 against each pattern in order until one matches. The matching pattern
455 executes its corresponding arm.
456
457 ~~~~
458 let my_number = 1;
459 match my_number {
460   0     => println!("zero"),
461   1 | 2 => println!("one or two"),
462   3..10 => println!("three to ten"),
463   _     => println!("something else")
464 }
465 ~~~~
466
467 Unlike in C, there is no "falling through" between arms: only one arm
468 executes, and it doesn't have to explicitly `break` out of the
469 construct when it is finished.
470
471 A `match` arm consists of a *pattern*, then a fat arrow `=>`, followed
472 by an *action* (expression). Each case is separated by commas. It is
473 often convenient to use a block expression for each case, in which case
474 the commas are optional as shown below. Literals are valid patterns and
475 match only their own value. A single arm may match multiple different
476 patterns by combining them with the pipe operator (`|`), so long as every
477 pattern binds the same set of variables. Ranges of numeric literal
478 patterns can be expressed with two dots, as in `M..N`. The underscore
479 (`_`) is a wildcard pattern that matches any single value. (`..`) is a
480 different wildcard that can match one or more fields in an `enum` variant.
481
482 ~~~
483 # let my_number = 1;
484 match my_number {
485   0 => { println!("zero") }
486   _ => { println!("something else") }
487 }
488 ~~~
489
490 `match` constructs must be *exhaustive*: they must have an arm
491 covering every possible case. For example, the typechecker would
492 reject the previous example if the arm with the wildcard pattern was
493 omitted.
494
495 A powerful application of pattern matching is *destructuring*:
496 matching in order to bind names to the contents of data types.
497
498 > *Note:* The following code makes use of tuples (`(f64, f64)`) which
499 > are explained in section 5.3. For now you can think of tuples as a list of
500 > items.
501
502 ~~~~
503 use std::f64;
504 fn angle(vector: (f64, f64)) -> f64 {
505     let pi = f64::consts::PI;
506     match vector {
507       (0.0, y) if y < 0.0 => 1.5 * pi,
508       (0.0, _) => 0.5 * pi,
509       (x, y) => (y / x).atan()
510     }
511 }
512 ~~~~
513
514 A variable name in a pattern matches any value, *and* binds that name
515 to the value of the matched value inside of the arm's action. Thus, `(0.0,
516 y)` matches any tuple whose first element is zero, and binds `y` to
517 the second element. `(x, y)` matches any two-element tuple, and binds both
518 elements to variables. `(0.0,_)` matches any tuple whose first element is zero
519 and does not bind anything to the second element.
520
521 A subpattern can also be bound to a variable, using `variable @ pattern`. For
522 example:
523
524 ~~~~
525 # let age = 23;
526 match age {
527     a @ 0..20 => println!("{} years old", a),
528     _ => println!("older than 21")
529 }
530 ~~~~
531
532 Any `match` arm can have a guard clause (written `if EXPR`), called a
533 *pattern guard*, which is an expression of type `bool` that
534 determines, after the pattern is found to match, whether the arm is
535 taken or not. The variables bound by the pattern are in scope in this
536 guard expression. The first arm in the `angle` example shows an
537 example of a pattern guard.
538
539 You've already seen simple `let` bindings, but `let` is a little
540 fancier than you've been led to believe. It, too, supports destructuring
541 patterns. For example, you can write this to extract the fields from a
542 tuple, introducing two variables at once: `a` and `b`.
543
544 ~~~~
545 # fn get_tuple_of_two_ints() -> (int, int) { (1, 1) }
546 let (a, b) = get_tuple_of_two_ints();
547 ~~~~
548
549 Let bindings only work with _irrefutable_ patterns: that is, patterns
550 that can never fail to match. This excludes `let` from matching
551 literals and most `enum` variants.
552
553 ## Loops
554
555 `while` denotes a loop that iterates as long as its given condition
556 (which must have type `bool`) evaluates to `true`. Inside a loop, the
557 keyword `break` aborts the loop, and `continue` aborts the current
558 iteration and continues with the next.
559
560 ~~~~
561 let mut cake_amount = 8;
562 while cake_amount > 0 {
563     cake_amount -= 1;
564 }
565 ~~~~
566
567 `loop` denotes an infinite loop, and is the preferred way of writing `while true`:
568
569 ~~~~
570 let mut x = 5u;
571 loop {
572     x += x - 3;
573     if x % 5 == 0 { break; }
574     println!("{}", x);
575 }
576 ~~~~
577
578 This code prints out a weird sequence of numbers and stops as soon as
579 it finds one that can be divided by five.
580
581 There is also a for-loop that can be used to iterate over a range of numbers:
582
583 ~~~~
584 for n in range(0, 5) {
585     println!("{}", n);
586 }
587 ~~~~
588
589 The snippet above prints integer numbers under 5 starting at 0.
590
591 More generally, a for loop works with anything implementing the `Iterator` trait.
592 Data structures can provide one or more methods that return iterators over
593 their contents. For example, strings support iteration over their contents in
594 various ways:
595
596 ~~~~
597 let s = "Hello";
598 for c in s.chars() {
599     println!("{}", c);
600 }
601 ~~~~
602
603 The snippet above prints the characters in "Hello" vertically, adding a new
604 line after each character.
605
606
607 # Data structures
608
609 ## Structs
610
611 Rust struct types must be declared before they are used using the `struct`
612 syntax: `struct Name { field1: T1, field2: T2 [, ...] }`, where `T1`, `T2`,
613 ... denote types. To construct a struct, use the same syntax, but leave off
614 the `struct`: for example: `Point { x: 1.0, y: 2.0 }`.
615
616 Structs are quite similar to C structs and are even laid out the same way in
617 memory (so you can read from a Rust struct in C, and vice-versa). Use the dot
618 operator to access struct fields, as in `mypoint.x`.
619
620 ~~~~
621 struct Point {
622     x: f64,
623     y: f64
624 }
625 ~~~~
626
627 Structs have "inherited mutability", which means that any field of a struct
628 may be mutable, if the struct is in a mutable slot.
629
630 With a value (say, `mypoint`) of such a type in a mutable location, you can do
631 `mypoint.y += 1.0`. But in an immutable location, such an assignment to a
632 struct without inherited mutability would result in a type error.
633
634 ~~~~ {.ignore}
635 # struct Point { x: f64, y: f64 }
636 let mut mypoint = Point { x: 1.0, y: 1.0 };
637 let origin = Point { x: 0.0, y: 0.0 };
638
639 mypoint.y += 1.0; // `mypoint` is mutable, and its fields as well
640 origin.y += 1.0; // ERROR: assigning to immutable field
641 ~~~~
642
643 `match` patterns destructure structs. The basic syntax is
644 `Name { fieldname: pattern, ... }`:
645
646 ~~~~
647 # struct Point { x: f64, y: f64 }
648 # let mypoint = Point { x: 0.0, y: 0.0 };
649 match mypoint {
650     Point { x: 0.0, y: yy } => println!("{}", yy),
651     Point { x: xx,  y: yy } => println!("{} {}", xx, yy)
652 }
653 ~~~~
654
655 In general, the field names of a struct do not have to appear in the same
656 order they appear in the type. When you are not interested in all
657 the fields of a struct, a struct pattern may end with `, ..` (as in
658 `Name { field1, .. }`) to indicate that you're ignoring all other fields.
659 Additionally, struct fields have a shorthand matching form that simply
660 reuses the field name as the binding name.
661
662 ~~~
663 # struct Point { x: f64, y: f64 }
664 # let mypoint = Point { x: 0.0, y: 0.0 };
665 match mypoint {
666     Point { x, .. } => println!("{}", x)
667 }
668 ~~~
669
670 ## Enums
671
672 Enums are datatypes with several alternate representations. A simple `enum`
673 defines one or more constants, all of which have the same type:
674
675 ~~~~
676 enum Direction {
677     North,
678     East,
679     South,
680     West
681 }
682 ~~~~
683
684 Each variant of this enum has a unique and constant integral discriminator
685 value. If no explicit discriminator is specified for a variant, the value
686 defaults to the value of the previous variant plus one. If the first variant
687 does not have a discriminator, it defaults to 0. For example, the value of
688 `North` is 0, `East` is 1, `South` is 2, and `West` is 3.
689
690 When an enum has simple integer discriminators, you can apply the `as` cast
691 operator to convert a variant to its discriminator value as an `int`:
692
693 ~~~~
694 # enum Direction { North }
695 println!( "{:?} => {}", North, North as int );
696 ~~~~
697
698 It is possible to set the discriminator values to chosen constant values:
699
700 ~~~~
701 enum Color {
702   Red = 0xff0000,
703   Green = 0x00ff00,
704   Blue = 0x0000ff
705 }
706 ~~~~
707
708 Variants do not have to be simple values; they may be more complex:
709
710 ~~~~
711 # struct Point { x: f64, y: f64 }
712 enum Shape {
713     Circle(Point, f64),
714     Rectangle(Point, Point)
715 }
716 ~~~~
717
718 A value of this type is either a `Circle`, in which case it contains a
719 `Point` struct and a f64, or a `Rectangle`, in which case it contains
720 two `Point` structs. The run-time representation of such a value
721 includes an identifier of the actual form that it holds, much like the
722 "tagged union" pattern in C, but with better static guarantees.
723
724 This declaration defines a type `Shape` that can refer to such shapes, and two
725 functions, `Circle` and `Rectangle`, which can be used to construct values of
726 the type. To create a new Circle, write `Circle(Point { x: 0.0, y: 0.0 },
727 10.0)`.
728
729 All of these variant constructors may be used as patterns. The only way to
730 access the contents of an enum instance is the destructuring of a match. For
731 example:
732
733 ~~~~
734 use std::f64;
735 # struct Point {x: f64, y: f64}
736 # enum Shape { Circle(Point, f64), Rectangle(Point, Point) }
737 fn area(sh: Shape) -> f64 {
738     match sh {
739         Circle(_, size) => f64::consts::PI * size * size,
740         Rectangle(Point { x, y }, Point { x: x2, y: y2 }) => (x2 - x) * (y2 - y)
741     }
742 }
743 ~~~~
744
745 Use a lone `_` to ignore an individual field. Ignore all fields of a variant
746 like: `Circle(..)`. Nullary enum patterns are written without parentheses:
747
748 ~~~~
749 # struct Point { x: f64, y: f64 }
750 # enum Direction { North, East, South, West }
751 fn point_from_direction(dir: Direction) -> Point {
752     match dir {
753         North => Point { x:  0.0, y:  1.0 },
754         East  => Point { x:  1.0, y:  0.0 },
755         South => Point { x:  0.0, y: -1.0 },
756         West  => Point { x: -1.0, y:  0.0 }
757     }
758 }
759 ~~~~
760
761 Enum variants may also be structs. For example:
762
763 ~~~~
764 use std::f64;
765 # struct Point { x: f64, y: f64 }
766 # fn square(x: f64) -> f64 { x * x }
767 enum Shape {
768     Circle { center: Point, radius: f64 },
769     Rectangle { top_left: Point, bottom_right: Point }
770 }
771 fn area(sh: Shape) -> f64 {
772     match sh {
773         Circle { radius: radius, .. } => f64::consts::PI * square(radius),
774         Rectangle { top_left: top_left, bottom_right: bottom_right } => {
775             (bottom_right.x - top_left.x) * (top_left.y - bottom_right.y)
776         }
777     }
778 }
779 ~~~~
780
781 > *Note:* This feature of the compiler is currently gated behind the
782 > `#[feature(struct_variant)]` directive. More about these directives can be
783 > found in the manual.
784
785 ## Tuples
786
787 Tuples in Rust behave exactly like structs, except that their fields do not
788 have names. Thus, you cannot access their fields with dot notation.  Tuples
789 can have any arity (number of elements) except for 0 (though you may consider
790 unit, `()`, as the empty tuple if you like).
791
792 ~~~~
793 let mytup: (int, int, f64) = (10, 20, 30.0);
794 match mytup {
795   (a, b, c) => println!("{}", a + b + (c as int))
796 }
797 ~~~~
798
799 ## Tuple structs
800
801 Rust also has _tuple structs_, which behave like both structs and tuples,
802 except that, unlike tuples, tuple structs have names (so `Foo(1, 2)` has a
803 different type from `Bar(1, 2)`), and tuple structs' _fields_ do not have
804 names.
805
806 For example:
807
808 ~~~~
809 struct MyTup(int, int, f64);
810 let mytup: MyTup = MyTup(10, 20, 30.0);
811 match mytup {
812   MyTup(a, b, c) => println!("{}", a + b + (c as int))
813 }
814 ~~~~
815
816 <a name="newtype"></a>
817
818 There is a special case for tuple structs with a single field, which are
819 sometimes called "newtypes" (after Haskell's "newtype" feature). These are
820 used to define new types in such a way that the new name is not just a
821 synonym for an existing type but is rather its own distinct type.
822
823 ~~~~
824 struct GizmoId(int);
825 ~~~~
826
827 Types like this can be useful to differentiate between data that have
828 the same underlying type but must be used in different ways.
829
830 ~~~~
831 struct Inches(int);
832 struct Centimeters(int);
833 ~~~~
834
835 The above definitions allow for a simple way for programs to avoid
836 confusing numbers that correspond to different units. Their integer
837 values can be extracted with pattern matching:
838
839 ~~~
840 # struct Inches(int);
841 let length_with_unit = Inches(10);
842 let Inches(integer_length) = length_with_unit;
843 println!("length is {} inches", integer_length);
844 ~~~
845
846 # Functions
847
848 We've already seen several function definitions. Like all other static
849 declarations, such as `type`, functions can be declared both at the
850 top level and inside other functions (or in modules, which we'll come
851 back to [later](#crates-and-the-module-system)). The `fn` keyword introduces a
852 function. A function has an argument list, which is a parenthesized
853 list of `name: type` pairs separated by commas. An arrow `->`
854 separates the argument list and the function's return type.
855
856 ~~~~
857 fn line(a: int, b: int, x: int) -> int {
858     return a * x + b;
859 }
860 ~~~~
861
862 The `return` keyword immediately returns from the body of a function. It
863 is optionally followed by an expression to return. A function can
864 also return a value by having its top-level block produce an
865 expression.
866
867 ~~~~
868 fn line(a: int, b: int, x: int) -> int {
869     a * x + b
870 }
871 ~~~~
872
873 It's better Rust style to write a return value this way instead of
874 writing an explicit `return`. The utility of `return` comes in when
875 returning early from a function. Functions that do not return a value
876 are said to return unit, `()`, and both the return type and the return
877 value may be omitted from the definition. The following two functions
878 are equivalent.
879
880 ~~~~
881 fn do_nothing_the_hard_way() -> () { return (); }
882
883 fn do_nothing_the_easy_way() { }
884 ~~~~
885
886 Ending the function with a semicolon like so is equivalent to returning `()`.
887
888 ~~~~
889 fn line(a: int, b: int, x: int) -> int { a * x + b  }
890 fn oops(a: int, b: int, x: int) -> ()  { a * x + b; }
891
892 assert!(8 == line(5, 3, 1));
893 assert!(() == oops(5, 3, 1));
894 ~~~~
895
896 As with `match` expressions and `let` bindings, function arguments support
897 pattern destructuring. Like `let`, argument patterns must be irrefutable,
898 as in this example that unpacks the first value from a tuple and returns it.
899
900 ~~~
901 fn first((value, _): (int, f64)) -> int { value }
902 ~~~
903
904 # Destructors
905
906 A *destructor* is a function responsible for cleaning up the resources used by
907 an object when it is no longer accessible. Destructors can be defined to handle
908 the release of resources like files, sockets and heap memory.
909
910 Objects are never accessible after their destructor has been called, so no
911 dynamic failures are possible from accessing freed resources. When a task
912 fails, destructors of all objects in the task are called.
913
914 The `box` operator performs memory allocation on the heap:
915
916 ~~~~
917 {
918     // an integer allocated on the heap
919     let y = box 10;
920 }
921 // the destructor frees the heap memory as soon as `y` goes out of scope
922 ~~~~
923
924 Rust includes syntax for heap memory allocation in the language since it's
925 commonly used, but the same semantics can be implemented by a type with a
926 custom destructor.
927
928 # Ownership
929
930 Rust formalizes the concept of object ownership to delegate management of an
931 object's lifetime to either a variable or a task-local garbage collector. An
932 object's owner is responsible for managing the lifetime of the object by
933 calling the destructor, and the owner determines whether the object is mutable.
934
935 Ownership is recursive, so mutability is inherited recursively and a destructor
936 destroys the contained tree of owned objects. Variables are top-level owners
937 and destroy the contained object when they go out of scope.
938
939 ~~~~
940 // the struct owns the objects contained in the `x` and `y` fields
941 struct Foo { x: int, y: Box<int> }
942
943 {
944     // `a` is the owner of the struct, and thus the owner of the struct's fields
945     let a = Foo { x: 5, y: box 10 };
946 }
947 // when `a` goes out of scope, the destructor for the `Box<int>` in the struct's
948 // field is called
949
950 // `b` is mutable, and the mutability is inherited by the objects it owns
951 let mut b = Foo { x: 5, y: box 10 };
952 b.x = 10;
953 ~~~~
954
955 If an object doesn't contain any non-Send types, it consists of a single
956 ownership tree and is itself given the `Send` trait which allows it to be sent
957 between tasks. Custom destructors can only be implemented directly on types
958 that are `Send`, but non-`Send` types can still *contain* types with custom
959 destructors. Example of types which are not `Send` are [`Gc<T>`][gc] and
960 [`Rc<T>`][rc], the shared-ownership types.
961
962 [gc]: http://doc.rust-lang.org/std/gc/struct.Gc.html
963 [rc]: http://doc.rust-lang.org/std/rc/struct.Rc.html
964
965 # Implementing a linked list
966
967 An `enum` is a natural fit for describing a linked list, because it can express
968 a `List` type as being *either* the end of the list (`Nil`) or another node
969 (`Cons`). The full definition of the `Cons` variant will require some thought.
970
971 ~~~ {.ignore}
972 enum List {
973     Cons(...), // an incomplete definition of the next element in a List
974     Nil        // the end of a List
975 }
976 ~~~
977
978 The obvious approach is to define `Cons` as containing an element in the list
979 along with the next `List` node. However, this will generate a compiler error.
980
981 ~~~ {.ignore}
982 // error: illegal recursive enum type; wrap the inner value in a box to make it
983 // representable
984 enum List {
985     Cons(u32, List), // an element (`u32`) and the next node in the list
986     Nil
987 }
988 ~~~
989
990 This error message is related to Rust's precise control over memory layout, and
991 solving it will require introducing the concept of *boxing*.
992
993 ## Boxes
994
995 A value in Rust is stored directly inside the owner. If a `struct` contains
996 four `u32` fields, it will be four times as large as a single `u32`.
997
998 ~~~
999 use std::mem::size_of; // bring `size_of` into the current scope, for convenience
1000
1001 struct Foo {
1002     a: u32,
1003     b: u32,
1004     c: u32,
1005     d: u32
1006 }
1007
1008 assert_eq!(size_of::<Foo>(), size_of::<u32>() * 4);
1009
1010 struct Bar {
1011     a: Foo,
1012     b: Foo,
1013     c: Foo,
1014     d: Foo
1015 }
1016
1017 assert_eq!(size_of::<Bar>(), size_of::<u32>() * 16);
1018 ~~~
1019
1020 Our previous attempt at defining the `List` type included an `u32` and a `List`
1021 directly inside `Cons`, making it at least as big as the sum of both types. The
1022 type was invalid because the size was infinite!
1023
1024 An *owned box* (`Box`, located in the `std::owned` module) uses a dynamic memory
1025 allocation to provide the invariant of always being the size of a pointer,
1026 regardless of the contained type. This can be leveraged to create a valid `List`
1027 definition:
1028
1029 ~~~
1030
1031 enum List {
1032     Cons(u32, Box<List>),
1033     Nil
1034 }
1035 ~~~
1036
1037 Defining a recursive data structure like this is the canonical example of an
1038 owned box. Much like an unboxed value, an owned box has a single owner and is
1039 therefore limited to expressing a tree-like data structure.
1040
1041 Consider an instance of our `List` type:
1042
1043 ~~~
1044 # enum List {
1045 #     Cons(u32, Box<List>),
1046 #     Nil
1047 # }
1048 let list = Cons(1, box Cons(2, box Cons(3, box Nil)));
1049 ~~~
1050
1051 It represents an owned tree of values, inheriting mutability down the tree and
1052 being destroyed along with the owner. Since the `list` variable above is
1053 immutable, the whole list is immutable. The memory allocation itself is the
1054 box, while the owner holds onto a pointer to it:
1055
1056 ~~~ {.notrust}
1057             List box            List box            List box          List box
1058         +--------------+    +--------------+    +--------------+    +----------+
1059 list -> | Cons | 1 |   | -> | Cons | 2 |   | -> | Cons | 3 |   | -> | Nil      |
1060         +--------------+    +--------------+    +--------------+    +----------+
1061 ~~~
1062
1063 > *Note:* the above diagram shows the logical contents of the enum. The actual
1064 > memory layout of the enum may vary. For example, for the `List` enum shown
1065 > above, Rust guarantees that there will be no enum tag field in the actual
1066 > structure. See the language reference for more details.
1067
1068 An owned box is a common example of a type with a destructor. The allocated
1069 memory is cleaned up when the box is destroyed.
1070
1071 ## Move semantics
1072
1073 Rust uses a shallow copy for parameter passing, assignment and returning from
1074 functions. Passing around the `List` will copy only as deep as the pointer to
1075 the box rather than doing an implicit heap allocation.
1076
1077 ~~~
1078 # enum List {
1079 #     Cons(u32, Box<List>),
1080 #     Nil
1081 # }
1082 let xs = Cons(1, box Cons(2, box Cons(3, box Nil)));
1083 let ys = xs; // copies `Cons(u32, pointer)` shallowly
1084 ~~~
1085
1086 Rust will consider a shallow copy of a type with a destructor like `List` to
1087 *move ownership* of the value. After a value has been moved, the source
1088 location cannot be used unless it is reinitialized.
1089
1090 ~~~
1091 # enum List {
1092 #     Cons(u32, Box<List>),
1093 #     Nil
1094 # }
1095 let mut xs = Nil;
1096 let ys = xs;
1097
1098 // attempting to use `xs` will result in an error here
1099
1100 xs = Nil;
1101
1102 // `xs` can be used again
1103 ~~~
1104
1105 A destructor call will only occur for a variable that has not been moved from,
1106 as it is only called a single time.
1107
1108
1109 Avoiding a move can be done with the library-defined `clone` method:
1110
1111 ~~~~
1112 let x = box 5;
1113 let y = x.clone(); // `y` is a newly allocated box
1114 let z = x; // no new memory allocated, `x` can no longer be used
1115 ~~~~
1116
1117 The `clone` method is provided by the `Clone` trait, and can be derived for
1118 our `List` type. Traits will be explained in detail [later](#traits).
1119
1120 ~~~{.ignore}
1121 #[deriving(Clone)]
1122 enum List {
1123     Cons(u32, Box<List>),
1124     Nil
1125 }
1126
1127 let x = Cons(5, box Nil);
1128 let y = x.clone();
1129
1130 // `x` can still be used!
1131
1132 let z = x;
1133
1134 // and now, it can no longer be used since it has been moved
1135 ~~~
1136
1137 The mutability of a value may be changed by moving it to a new owner:
1138
1139 ~~~~
1140 let r = box 13;
1141 let mut s = r; // box becomes mutable
1142 *s += 1;
1143 let t = s; // box becomes immutable
1144 ~~~~
1145
1146 A simple way to define a function prepending to the `List` type is to take
1147 advantage of moves:
1148
1149 ~~~
1150 enum List {
1151     Cons(u32, Box<List>),
1152     Nil
1153 }
1154
1155 fn prepend(xs: List, value: u32) -> List {
1156     Cons(value, box xs)
1157 }
1158
1159 let mut xs = Nil;
1160 xs = prepend(xs, 1);
1161 xs = prepend(xs, 2);
1162 xs = prepend(xs, 3);
1163 ~~~
1164
1165 However, this is not a very flexible definition of `prepend` as it requires
1166 ownership of a list to be passed in rather than just mutating it in-place.
1167
1168 ## References
1169
1170 The obvious signature for a `List` equality comparison is the following:
1171
1172 ~~~{.ignore}
1173 fn eq(xs: List, ys: List) -> bool { /* ... */ }
1174 ~~~
1175
1176 However, this will cause both lists to be moved into the function. Ownership
1177 isn't required to compare the lists, so the function should take *references*
1178 (&T) instead.
1179
1180 ~~~{.ignore}
1181 fn eq(xs: &List, ys: &List) -> bool { /* ... */ }
1182 ~~~
1183
1184 A reference is a *non-owning* view of a value. A reference can be obtained with the `&` (address-of)
1185 operator. It can be dereferenced by using the `*` operator. In a pattern, such as `match` expression
1186 branches, the `ref` keyword can be used to bind to a variable name by-reference rather than
1187 by-value. A recursive definition of equality using references is as follows:
1188
1189 ~~~
1190 # enum List {
1191 #     Cons(u32, Box<List>),
1192 #     Nil
1193 # }
1194 fn eq(xs: &List, ys: &List) -> bool {
1195     // Match on the next node in both lists.
1196     match (xs, ys) {
1197         // If we have reached the end of both lists, they are equal.
1198         (&Nil, &Nil) => true,
1199         // If the current elements of both lists are equal, keep going.
1200         (&Cons(x, box ref next_xs), &Cons(y, box ref next_ys))
1201                 if x == y => eq(next_xs, next_ys),
1202         // If the current elements are not equal, the lists are not equal.
1203         _ => false
1204     }
1205 }
1206
1207 let xs = Cons(5, box Cons(10, box Nil));
1208 let ys = Cons(5, box Cons(10, box Nil));
1209 assert!(eq(&xs, &ys));
1210 ~~~
1211
1212 > *Note:* Rust doesn't guarantee [tail-call](http://en.wikipedia.org/wiki/Tail_call) optimization,
1213 > but LLVM is able to handle a simple case like this with optimizations enabled.
1214
1215 ## Lists of other types
1216
1217 Our `List` type is currently always a list of 32-bit unsigned integers. By
1218 leveraging Rust's support for generics, it can be extended to work for any
1219 element type.
1220
1221 The `u32` in the previous definition can be substituted with a type parameter:
1222
1223 > *Note:* The following code introduces generics, which are explained in a
1224 > [dedicated section](#generics).
1225
1226 ~~~
1227 enum List<T> {
1228     Cons(T, Box<List<T>>),
1229     Nil
1230 }
1231 ~~~
1232
1233 The old `List` of `u32` is now available as `List<u32>`. The `prepend`
1234 definition has to be updated too:
1235
1236 ~~~
1237 # enum List<T> {
1238 #     Cons(T, Box<List<T>>),
1239 #     Nil
1240 # }
1241 fn prepend<T>(xs: List<T>, value: T) -> List<T> {
1242     Cons(value, box xs)
1243 }
1244 ~~~
1245
1246 Generic functions and types like this are equivalent to defining specialized
1247 versions for each set of type parameters.
1248
1249 Using the generic `List<T>` works much like before, thanks to type inference:
1250
1251 ~~~
1252 # enum List<T> {
1253 #     Cons(T, Box<List<T>>),
1254 #     Nil
1255 # }
1256 # fn prepend<T>(xs: List<T>, value: T) -> List<T> {
1257 #     Cons(value, box xs)
1258 # }
1259 let mut xs = Nil; // Unknown type! This is a `List<T>`, but `T` can be anything.
1260 xs = prepend(xs, 10); // Here the compiler infers `xs`'s type as `List<int>`.
1261 xs = prepend(xs, 15);
1262 xs = prepend(xs, 20);
1263 ~~~
1264
1265 The code sample above demonstrates type inference making most type annotations optional. It is
1266 equivalent to the following type-annotated code:
1267
1268 ~~~
1269 # enum List<T> {
1270 #     Cons(T, Box<List<T>>),
1271 #     Nil
1272 # }
1273 # fn prepend<T>(xs: List<T>, value: T) -> List<T> {
1274 #     Cons(value, box xs)
1275 # }
1276 let mut xs: List<int> = Nil::<int>;
1277 xs = prepend::<int>(xs, 10);
1278 xs = prepend::<int>(xs, 15);
1279 xs = prepend::<int>(xs, 20);
1280 ~~~
1281
1282 In declarations, the language uses `Type<T, U, V>` to describe a list of type
1283 parameters, but expressions use `identifier::<T, U, V>`, to disambiguate the
1284 `<` operator.
1285
1286 ## Defining list equality with generics
1287
1288 Generic functions are type-checked from the definition, so any necessary properties of the type must
1289 be specified up-front. Our previous definition of list equality relied on the element type having
1290 the `==` operator available, and took advantage of the lack of a destructor on `u32` to copy it
1291 without a move of ownership.
1292
1293 We can add a *trait bound* on the `Eq` trait to require that the type implement the `==` operator.
1294 Two more `ref` annotations need to be added to avoid attempting to move out the element types:
1295
1296 ~~~
1297 # enum List<T> {
1298 #     Cons(T, Box<List<T>>),
1299 #     Nil
1300 # }
1301 fn eq<T: Eq>(xs: &List<T>, ys: &List<T>) -> bool {
1302     // Match on the next node in both lists.
1303     match (xs, ys) {
1304         // If we have reached the end of both lists, they are equal.
1305         (&Nil, &Nil) => true,
1306         // If the current elements of both lists are equal, keep going.
1307         (&Cons(ref x, box ref next_xs), &Cons(ref y, box ref next_ys))
1308                 if x == y => eq(next_xs, next_ys),
1309         // If the current elements are not equal, the lists are not equal.
1310         _ => false
1311     }
1312 }
1313
1314 let xs = Cons('c', box Cons('a', box Cons('t', box Nil)));
1315 let ys = Cons('c', box Cons('a', box Cons('t', box Nil)));
1316 assert!(eq(&xs, &ys));
1317 ~~~
1318
1319 This would be a good opportunity to implement the `Eq` trait for our list type, making the `==` and
1320 `!=` operators available. We'll need to provide an `impl` for the `Eq` trait and a definition of the
1321 `eq` method. In a method, the `self` parameter refers to an instance of the type we're implementing
1322 on.
1323
1324 ~~~
1325 # enum List<T> {
1326 #     Cons(T, Box<List<T>>),
1327 #     Nil
1328 # }
1329 impl<T: Eq> Eq for List<T> {
1330     fn eq(&self, ys: &List<T>) -> bool {
1331         // Match on the next node in both lists.
1332         match (self, ys) {
1333             // If we have reached the end of both lists, they are equal.
1334             (&Nil, &Nil) => true,
1335             // If the current elements of both lists are equal, keep going.
1336             (&Cons(ref x, box ref next_xs), &Cons(ref y, box ref next_ys))
1337                     if x == y => next_xs == next_ys,
1338             // If the current elements are not equal, the lists are not equal.
1339             _ => false
1340         }
1341     }
1342 }
1343
1344 let xs = Cons(5, box Cons(10, box Nil));
1345 let ys = Cons(5, box Cons(10, box Nil));
1346 // The methods below are part of the Eq trait,
1347 // which we implemented on our linked list.
1348 assert!(xs.eq(&ys));
1349 assert!(!xs.ne(&ys));
1350
1351 // The Eq trait also allows us to use the shorthand infix operators.
1352 assert!(xs == ys);    // `xs == ys` is short for `xs.eq(&ys)`
1353 assert!(!(xs != ys)); // `xs != ys` is short for `xs.ne(&ys)`
1354 ~~~
1355
1356 # More on boxes
1357
1358 The most common use case for owned boxes is creating recursive data structures
1359 like a binary search tree. Rust's trait-based generics system (covered later in
1360 the tutorial) is usually used for static dispatch, but also provides dynamic
1361 dispatch via boxing. Values of different types may have different sizes, but a
1362 box is able to *erase* the difference via the layer of indirection they
1363 provide.
1364
1365 In uncommon cases, the indirection can provide a performance gain or memory
1366 reduction by making values smaller. However, unboxed values should almost
1367 always be preferred when they are usable.
1368
1369 Note that returning large unboxed values via boxes is unnecessary. A large
1370 value is returned via a hidden output parameter, and the decision on where to
1371 place the return value should be left to the caller:
1372
1373 ~~~~
1374 fn foo() -> (u64, u64, u64, u64, u64, u64) {
1375     (5, 5, 5, 5, 5, 5)
1376 }
1377
1378 let x = box foo(); // allocates a box, and writes the integers directly to it
1379 ~~~~
1380
1381 Beyond the properties granted by the size, an owned box behaves as a regular
1382 value by inheriting the mutability and lifetime of the owner:
1383
1384 ~~~~
1385 let x = 5; // immutable
1386 let mut y = 5; // mutable
1387 y += 2;
1388
1389 let x = box 5; // immutable
1390 let mut y = box 5; // mutable
1391 *y += 2; // the `*` operator is needed to access the contained value
1392 ~~~~
1393
1394 # References
1395
1396 In contrast with
1397 owned boxes, where the holder of an owned box is the owner of the pointed-to
1398 memory, references never imply ownership - they are "borrowed".
1399 You can borrow a reference to
1400 any object, and the compiler verifies that it cannot outlive the lifetime of
1401 the object.
1402
1403 As an example, consider a simple struct type, `Point`:
1404
1405 ~~~
1406 struct Point {
1407     x: f64,
1408     y: f64
1409 }
1410 ~~~
1411
1412 We can use this simple definition to allocate points in many different
1413 ways. For example, in this code, each of these local variables
1414 contains a point, but allocated in a different location:
1415
1416 ~~~
1417 # struct Point { x: f64, y: f64 }
1418 let on_the_stack :     Point  =     Point { x: 3.0, y: 4.0 };
1419 let owned_box    : Box<Point> = box Point { x: 7.0, y: 9.0 };
1420 ~~~
1421
1422 Suppose we want to write a procedure that computes the distance
1423 between any two points, no matter where they are stored. One option is
1424 to define a function that takes two arguments of type point—that is,
1425 it takes the points by value. But this will cause the points to be
1426 copied when we call the function. For points, this is probably not so
1427 bad, but often copies are expensive. So we’d like to define a function
1428 that takes the points by pointer. We can use references to do this:
1429
1430 ~~~
1431 # struct Point { x: f64, y: f64 }
1432 fn compute_distance(p1: &Point, p2: &Point) -> f64 {
1433     let x_d = p1.x - p2.x;
1434     let y_d = p1.y - p2.y;
1435     (x_d * x_d + y_d * y_d).sqrt()
1436 }
1437 ~~~
1438
1439 Now we can call `compute_distance()` in various ways:
1440
1441 ~~~
1442 # struct Point{ x: f64, y: f64 };
1443 # let on_the_stack :     Point  =     Point { x: 3.0, y: 4.0 };
1444 # let owned_box    : Box<Point> = box Point { x: 7.0, y: 9.0 };
1445 # fn compute_distance(p1: &Point, p2: &Point) -> f64 { 0.0 }
1446 compute_distance(&on_the_stack, owned_box);
1447 ~~~
1448
1449 Here the `&` operator is used to take the address of the variable
1450 `on_the_stack`; this is because `on_the_stack` has the type `Point`
1451 (that is, a struct value) and we have to take its address to get a
1452 reference. We also call this _borrowing_ the local variable
1453 `on_the_stack`, because we are creating an alias: that is, another
1454 route to the same data.
1455
1456 In the case of `owned_box`, however, no
1457 explicit action is necessary. The compiler will automatically convert
1458 a box `box point` to a reference like
1459 `&point`. This is another form of borrowing; in this case, the
1460 contents of the owned box are being lent out.
1461
1462 Whenever a value is borrowed, there are some limitations on what you
1463 can do with the original. For example, if the contents of a variable
1464 have been lent out, you cannot send that variable to another task, nor
1465 will you be permitted to take actions that might cause the borrowed
1466 value to be freed or to change its type. This rule should make
1467 intuitive sense: you must wait for a borrowed value to be returned
1468 (that is, for the reference to go out of scope) before you can
1469 make full use of it again.
1470
1471 For a more in-depth explanation of references and lifetimes, read the
1472 [references and lifetimes guide][lifetimes].
1473
1474 ## Freezing
1475
1476 Lending an &-pointer to an object freezes it and prevents mutation—even if the object was declared as `mut`.
1477 `Freeze` objects have freezing enforced statically at compile-time. An example
1478 of a non-`Freeze` type is [`RefCell<T>`][refcell].
1479
1480 ~~~~
1481 let mut x = 5;
1482 {
1483     let y = &x; // `x` is now frozen. It cannot be modified or re-assigned.
1484 }
1485 // `x` is now unfrozen again
1486 # x = 3;
1487 ~~~~
1488
1489 [refcell]: http://doc.rust-lang.org/std/cell/struct.RefCell.html
1490
1491 # Dereferencing pointers
1492
1493 Rust uses the unary star operator (`*`) to access the contents of a
1494 box or pointer, similarly to C.
1495
1496 ~~~
1497 let owned = box 10;
1498 let borrowed = &20;
1499
1500 let sum = *owned + *borrowed;
1501 ~~~
1502
1503 Dereferenced mutable pointers may appear on the left hand side of
1504 assignments. Such an assignment modifies the value that the pointer
1505 points to.
1506
1507 ~~~
1508 let mut owned = box 10;
1509
1510 let mut value = 20;
1511 let borrowed = &mut value;
1512
1513 *owned = *borrowed + 100;
1514 *borrowed = *owned + 1000;
1515 ~~~
1516
1517 Pointers have high operator precedence, but lower precedence than the
1518 dot operator used for field and method access. This precedence order
1519 can sometimes make code awkward and parenthesis-filled.
1520
1521 ~~~
1522 # struct Point { x: f64, y: f64 }
1523 # enum Shape { Rectangle(Point, Point) }
1524 # impl Shape { fn area(&self) -> int { 0 } }
1525 let start = box Point { x: 10.0, y: 20.0 };
1526 let end = box Point { x: (*start).x + 100.0, y: (*start).y + 100.0 };
1527 let rect = &Rectangle(*start, *end);
1528 let area = (*rect).area();
1529 ~~~
1530
1531 To combat this ugliness the dot operator applies _automatic pointer
1532 dereferencing_ to the receiver (the value on the left-hand side of the
1533 dot), so in most cases, explicitly dereferencing the receiver is not necessary.
1534
1535 ~~~
1536 # struct Point { x: f64, y: f64 }
1537 # enum Shape { Rectangle(Point, Point) }
1538 # impl Shape { fn area(&self) -> int { 0 } }
1539 let start = box Point { x: 10.0, y: 20.0 };
1540 let end = box Point { x: start.x + 100.0, y: start.y + 100.0 };
1541 let rect = &Rectangle(*start, *end);
1542 let area = rect.area();
1543 ~~~
1544
1545 You can write an expression that dereferences any number of pointers
1546 automatically. For example, if you feel inclined, you could write
1547 something silly like
1548
1549 ~~~
1550 # struct Point { x: f64, y: f64 }
1551 let point = &box Point { x: 10.0, y: 20.0 };
1552 println!("{:f}", point.x);
1553 ~~~
1554
1555 The indexing operator (`[]`) also auto-dereferences.
1556
1557 # Vectors and strings
1558
1559 A vector is a contiguous block of memory containing zero or more values of the
1560 same type. Rust also supports vector reference types, called slices, which are
1561 a view into a block of memory represented as a pointer and a length.
1562
1563 Strings are represented as vectors of `u8`, with the guarantee of containing a
1564 valid UTF-8 sequence.
1565
1566 Fixed-size vectors are an unboxed block of memory, with the element length as
1567 part of the type. A fixed-size vector owns the elements it contains, so the
1568 elements are mutable if the vector is mutable. Fixed-size strings do not exist.
1569
1570 ~~~
1571 // A fixed-size vector
1572 let numbers = [1, 2, 3];
1573 let more_numbers = numbers;
1574
1575 // The type of a fixed-size vector is written as `[Type, ..length]`
1576 let five_zeroes: [int, ..5] = [0, ..5];
1577 ~~~
1578
1579 A unique vector is dynamically sized, and has a destructor to clean up
1580 allocated memory on the heap. A unique vector owns the elements it contains, so
1581 the elements are mutable if the vector is mutable.
1582
1583 ~~~
1584 use std::strbuf::StrBuf;
1585
1586 // A dynamically sized vector (unique vector)
1587 let mut numbers = vec![1, 2, 3];
1588 numbers.push(4);
1589 numbers.push(5);
1590
1591 // The type of a unique vector is written as `Vec<int>`
1592 let more_numbers: Vec<int> = numbers.move_iter().map(|i| i+1).collect();
1593
1594 // The original `numbers` value can no longer be used, due to move semantics.
1595
1596 let mut string = StrBuf::from_str("fo");
1597 string.push_char('o');
1598 ~~~
1599
1600 Slices are similar to fixed-size vectors, but the length is not part of the
1601 type. They simply point into a block of memory and do not have ownership over
1602 the elements.
1603
1604 ~~~
1605 // A slice
1606 let xs = &[1, 2, 3];
1607
1608 // Slices have their type written as `&[int]`
1609 let ys: &[int] = xs;
1610
1611 // Other vector types coerce to slices
1612 let three = [1, 2, 3];
1613 let zs: &[int] = three;
1614
1615 // An unadorned string literal is an immutable string slice
1616 let string = "foobar";
1617
1618 // A string slice type is written as `&str`
1619 let view: &str = string.slice(0, 3);
1620 ~~~
1621
1622 Mutable slices also exist, just as there are mutable references. However, there
1623 are no mutable string slices. Strings are a multi-byte encoding (UTF-8) of
1624 Unicode code points, so they cannot be freely mutated without the ability to
1625 alter the length.
1626
1627 ~~~
1628 let mut xs = [1, 2, 3];
1629 let view = xs.mut_slice(0, 2);
1630 view[0] = 5;
1631
1632 // The type of a mutable slice is written as `&mut [T]`
1633 let ys: &mut [int] = &mut [1, 2, 3];
1634 ~~~
1635
1636 Square brackets denote indexing into a slice or fixed-size vector:
1637
1638 ~~~~
1639 # enum Crayon { Almond, AntiqueBrass, Apricot,
1640 #               Aquamarine, Asparagus, AtomicTangerine,
1641 #               BananaMania, Beaver, Bittersweet };
1642 # fn draw_scene(c: Crayon) { }
1643 let crayons: [Crayon, ..3] = [BananaMania, Beaver, Bittersweet];
1644 match crayons[0] {
1645     Bittersweet => draw_scene(crayons[0]),
1646     _ => ()
1647 }
1648 ~~~~
1649
1650 A slice or fixed-size vector can be destructured using pattern matching:
1651
1652 ~~~~
1653 let numbers: &[int] = &[1, 2, 3];
1654 let score = match numbers {
1655     [] => 0,
1656     [a] => a * 10,
1657     [a, b] => a * 6 + b * 4,
1658     [a, b, c, ..rest] => a * 5 + b * 3 + c * 2 + rest.len() as int
1659 };
1660 ~~~~
1661
1662 Both vectors and strings support a number of useful [methods](#methods),
1663 defined in [`std::vec`], [`std::slice`], and [`std::str`].
1664
1665 [`std::vec`]: std/vec/index.html
1666 [`std::slice`]: std/slice/index.html
1667 [`std::str`]: std/str/index.html
1668
1669 # Ownership escape hatches
1670
1671 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
1672 single parent ownership.
1673
1674 The standard library provides the `std::rc::Rc` pointer type to express *shared ownership* over a
1675 reference counted box. As soon as all of the `Rc` pointers go out of scope, the box and the
1676 contained value are destroyed.
1677
1678 ~~~
1679 use std::rc::Rc;
1680
1681 // A fixed-size array allocated in a reference-counted box
1682 let x = Rc::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1683 let y = x.clone(); // a new owner
1684 let z = x; // this moves `x` into `z`, rather than creating a new owner
1685
1686 assert!(*z == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1687
1688 // the variable is mutable, but not the contents of the box
1689 let mut a = Rc::new([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
1690 a = z;
1691 ~~~
1692
1693 A garbage collected pointer is provided via `std::gc::Gc`, with a task-local garbage collector
1694 having ownership of the box. It allows the creation of cycles, and the individual `Gc` pointers do
1695 not have a destructor.
1696
1697 ~~~
1698 use std::gc::Gc;
1699
1700 // A fixed-size array allocated in a garbage-collected box
1701 let x = Gc::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1702 let y = x; // does not perform a move, unlike with `Rc`
1703 let z = x;
1704
1705 assert!(*z.borrow() == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1706 ~~~
1707
1708 With shared ownership, mutability cannot be inherited so the boxes are always immutable. However,
1709 it's possible to use *dynamic* mutability via types like `std::cell::Cell` where freezing is handled
1710 via dynamic checks and can fail at runtime.
1711
1712 The `Rc` and `Gc` types are not sendable, so they cannot be used to share memory between tasks. Safe
1713 immutable and mutable shared memory is provided by the `sync::arc` module.
1714
1715 # Closures
1716
1717 Named functions, like those we've seen so far, may not refer to local
1718 variables declared outside the function: they do not close over their
1719 environment (sometimes referred to as "capturing" variables in their
1720 environment). For example, you couldn't write the following:
1721
1722 ~~~~ {.ignore}
1723 let x = 3;
1724
1725 // `fun` cannot refer to `x`
1726 fn fun() -> () { println!("{}", x); }
1727 ~~~~
1728
1729 A _closure_ does support accessing the enclosing scope; below we will create
1730 2 _closures_ (nameless functions). Compare how `||` replaces `()` and how
1731 they try to access `x`:
1732
1733 ~~~~ {.ignore}
1734 let x = 3;
1735
1736 // `fun` is an invalid definition
1737 fn  fun       () -> () { println!("{}", x) }  // cannot capture from enclosing scope
1738 let closure = || -> () { println!("{}", x) }; // can capture from enclosing scope
1739
1740 // `fun_arg` is an invalid definition
1741 fn  fun_arg       (arg: int) -> () { println!("{}", arg + x) }  // cannot capture
1742 let closure_arg = |arg: int| -> () { println!("{}", arg + x) }; // can capture
1743 //                      ^
1744 // Requires a type because the implementation needs to know which `+` to use.
1745 // In the future, the implementation may not need the help.
1746
1747 fun();          // Still won't work
1748 closure();      // Prints: 3
1749
1750 fun_arg(7);     // Still won't work
1751 closure_arg(7); // Prints: 10
1752 ~~~~
1753
1754 Closures begin with the argument list between vertical bars and are followed by
1755 a single expression. Remember that a block, `{ <expr1>; <expr2>; ... }`, is
1756 considered a single expression: it evaluates to the result of the last
1757 expression it contains if that expression is not followed by a semicolon,
1758 otherwise the block evaluates to `()`, the unit value.
1759
1760 In general, return types and all argument types must be specified
1761 explicitly for function definitions.  (As previously mentioned in the
1762 [Functions section](#functions), omitting the return type from a
1763 function declaration is synonymous with an explicit declaration of
1764 return type unit, `()`.)
1765
1766 ~~~~ {.ignore}
1767 fn  fun   (x: int)         { println!("{}", x) } // this is same as saying `-> ()`
1768 fn  square(x: int) -> uint { (x * x) as uint }   // other return types are explicit
1769
1770 // Error: mismatched types: expected `()` but found `uint`
1771 fn  badfun(x: int)         { (x * x) as uint }
1772 ~~~~
1773
1774 On the other hand, the compiler can usually infer both the argument
1775 and return types for a closure expression; therefore they are often
1776 omitted, since both a human reader and the compiler can deduce the
1777 types from the immediate context.  This is in contrast to function
1778 declarations, which require types to be specified and are not subject
1779 to type inference. Compare:
1780
1781 ~~~~ {.ignore}
1782 // `fun` as a function declaration cannot infer the type of `x`, so it must be provided
1783 fn  fun       (x: int) { println!("{}", x) }
1784 let closure = |x     | { println!("{}", x) }; // infers `x: int`, return type `()`
1785
1786 // For closures, omitting a return type is *not* synonymous with `-> ()`
1787 let add_3   = |y     | { 3i + y }; // infers `y: int`, return type `int`.
1788
1789 fun(10);            // Prints 10
1790 closure(20);        // Prints 20
1791 closure(add_3(30)); // Prints 33
1792
1793 fun("String"); // Error: mismatched types
1794
1795 // Error: mismatched types
1796 // inference already assigned `closure` the type `|int| -> ()`
1797 closure("String");
1798 ~~~~
1799
1800 In cases where the compiler needs assistance, the arguments and return
1801 types may be annotated on closures, using the same notation as shown
1802 earlier.  In the example below, since different types provide an
1803 implementation for the operator `*`, the argument type for the `x`
1804 parameter must be explicitly provided.
1805
1806 ~~~~{.ignore}
1807 // Error: the type of `x` must be known to be used with `x * x`
1808 let square = |x     | -> uint { (x * x) as uint };
1809 ~~~~
1810
1811 In the corrected version, the argument type is explicitly annotated,
1812 while the return type can still be inferred.
1813
1814 ~~~~
1815 let square_explicit = |x: int| -> uint { (x * x) as uint };
1816 let square_infer    = |x: int|         { (x * x) as uint };
1817
1818 println!("{}", square_explicit(20));  // 400
1819 println!("{}", square_infer(-20));    // 400
1820 ~~~~
1821
1822 There are several forms of closure, each with its own role. The most
1823 common, called a _stack closure_, has type `||` and can directly
1824 access local variables in the enclosing scope.
1825
1826 ~~~~
1827 let mut max = 0;
1828 let f = |x: int| if x > max { max = x };
1829 for x in [1, 2, 3].iter() {
1830     f(*x);
1831 }
1832 ~~~~
1833
1834 Stack closures are very efficient because their environment is
1835 allocated on the call stack and refers by pointer to captured
1836 locals. To ensure that stack closures never outlive the local
1837 variables to which they refer, stack closures are not
1838 first-class. That is, they can only be used in argument position; they
1839 cannot be stored in data structures or returned from
1840 functions. Despite these limitations, stack closures are used
1841 pervasively in Rust code.
1842
1843 ## Owned closures
1844
1845 Owned closures, written `proc`,
1846 hold on to things that can safely be sent between
1847 processes. They copy the values they close over,
1848 but they also own them: that is, no other code can access
1849 them. Owned closures are used in concurrent code, particularly
1850 for spawning [tasks][tasks].
1851
1852 Closures can be used to spawn tasks.
1853 A practical example of this pattern is found when using the `spawn` function,
1854 which starts a new task.
1855
1856 ~~~~
1857 use std::task::spawn;
1858
1859 // proc is the closure which will be spawned.
1860 spawn(proc() {
1861     println!("I'm a new task")
1862 });
1863 ~~~~
1864
1865 ## Closure compatibility
1866
1867 Rust closures have a convenient subtyping property: you can pass any kind of
1868 closure (as long as the arguments and return types match) to functions
1869 that expect a `||`. Thus, when writing a higher-order function that
1870 only calls its function argument, and does nothing else with it, you
1871 should almost always declare the type of that argument as `||`. That way,
1872 callers may pass any kind of closure.
1873
1874 ~~~~
1875 fn call_twice(f: ||) { f(); f(); }
1876 let closure = || { "I'm a closure, and it doesn't matter what type I am"; };
1877 fn function() { "I'm a normal function"; }
1878 call_twice(closure);
1879 call_twice(function);
1880 ~~~~
1881
1882 > *Note:* Both the syntax and the semantics will be changing
1883 > in small ways. At the moment they can be unsound in some
1884 > scenarios, particularly with non-copyable types.
1885
1886 # Methods
1887
1888 Methods are like functions except that they always begin with a special argument,
1889 called `self`,
1890 which has the type of the method's receiver. The
1891 `self` argument is like `this` in C++ and many other languages.
1892 Methods are called with dot notation, as in `my_vec.len()`.
1893
1894 _Implementations_, written with the `impl` keyword, can define
1895 methods on most Rust types, including structs and enums.
1896 As an example, let's define a `draw` method on our `Shape` enum.
1897
1898 ~~~
1899 # fn draw_circle(p: Point, f: f64) { }
1900 # fn draw_rectangle(p: Point, p: Point) { }
1901 struct Point {
1902     x: f64,
1903     y: f64
1904 }
1905
1906 enum Shape {
1907     Circle(Point, f64),
1908     Rectangle(Point, Point)
1909 }
1910
1911 impl Shape {
1912     fn draw(&self) {
1913         match *self {
1914             Circle(p, f) => draw_circle(p, f),
1915             Rectangle(p1, p2) => draw_rectangle(p1, p2)
1916         }
1917     }
1918 }
1919
1920 let s = Circle(Point { x: 1.0, y: 2.0 }, 3.0);
1921 s.draw();
1922 ~~~
1923
1924 This defines an _implementation_ for `Shape` containing a single
1925 method, `draw`. In most respects the `draw` method is defined
1926 like any other function, except for the name `self`.
1927
1928 The type of `self` is the type on which the method is implemented,
1929 or a pointer thereof. As an argument it is written either `self`,
1930 `&self`, or `~self`.
1931 A caller must in turn have a compatible pointer type to call the method.
1932
1933 ~~~
1934 # fn draw_circle(p: Point, f: f64) { }
1935 # fn draw_rectangle(p: Point, p: Point) { }
1936 # struct Point { x: f64, y: f64 }
1937 # enum Shape {
1938 #     Circle(Point, f64),
1939 #     Rectangle(Point, Point)
1940 # }
1941 impl Shape {
1942     fn draw_reference(&self) { /* ... */ }
1943     fn draw_owned(~self) { /* ... */ }
1944     fn draw_value(self) { /* ... */ }
1945 }
1946
1947 let s = Circle(Point { x: 1.0, y: 2.0 }, 3.0);
1948
1949 (&s).draw_reference();
1950 (box s).draw_owned();
1951 s.draw_value();
1952 ~~~
1953
1954 Methods typically take a reference self type,
1955 so the compiler will go to great lengths to convert a callee
1956 to a reference.
1957
1958 ~~~
1959 # fn draw_circle(p: Point, f: f64) { }
1960 # fn draw_rectangle(p: Point, p: Point) { }
1961 # struct Point { x: f64, y: f64 }
1962 # enum Shape {
1963 #     Circle(Point, f64),
1964 #     Rectangle(Point, Point)
1965 # }
1966 # impl Shape {
1967 #    fn draw_reference(&self) { /* ... */ }
1968 #    fn draw_owned(~self) { /* ... */ }
1969 #    fn draw_value(self) { /* ... */ }
1970 # }
1971 # let s = Circle(Point { x: 1.0, y: 2.0 }, 3.0);
1972 // As with typical function arguments, owned pointers
1973 // are automatically converted to references
1974
1975 (box s).draw_reference();
1976
1977 // Unlike typical function arguments, the self value will
1978 // automatically be referenced ...
1979 s.draw_reference();
1980
1981 // ... and dereferenced
1982 (& &s).draw_reference();
1983
1984 // ... and dereferenced and borrowed
1985 (&box s).draw_reference();
1986 ~~~
1987
1988 Implementations may also define standalone (sometimes called "static")
1989 methods. The absence of a `self` parameter distinguishes such methods.
1990 These methods are the preferred way to define constructor functions.
1991
1992 ~~~~ {.ignore}
1993 impl Circle {
1994     fn area(&self) -> f64 { /* ... */ }
1995     fn new(area: f64) -> Circle { /* ... */ }
1996 }
1997 ~~~~
1998
1999 To call such a method, just prefix it with the type name and a double colon:
2000
2001 ~~~~
2002 use std::f64::consts::PI;
2003 struct Circle { radius: f64 }
2004 impl Circle {
2005     fn new(area: f64) -> Circle { Circle { radius: (area / PI).sqrt() } }
2006 }
2007 let c = Circle::new(42.5);
2008 ~~~~
2009
2010 # Generics
2011
2012 Throughout this tutorial, we've been defining functions that act only
2013 on specific data types. With type parameters we can also define
2014 functions whose arguments have generic types, and which can be invoked
2015 with a variety of types. Consider a generic `map` function, which
2016 takes a function `function` and a vector `vector` and returns a new
2017 vector consisting of the result of applying `function` to each element
2018 of `vector`:
2019
2020 ~~~~
2021 fn map<T, U>(vector: &[T], function: |v: &T| -> U) -> Vec<U> {
2022     let mut accumulator = Vec::new();
2023     for element in vector.iter() {
2024         accumulator.push(function(element));
2025     }
2026     return accumulator;
2027 }
2028 ~~~~
2029
2030 When defined with type parameters, as denoted by `<T, U>`, this
2031 function can be applied to any type of vector, as long as the type of
2032 `function`'s argument and the type of the vector's contents agree with
2033 each other.
2034
2035 Inside a generic function, the names of the type parameters
2036 (capitalized by convention) stand for opaque types. All you can do
2037 with instances of these types is pass them around: you can't apply any
2038 operations to them or pattern-match on them. Note that instances of
2039 generic types are often passed by pointer. For example, the parameter
2040 `function()` is supplied with a pointer to a value of type `T` and not
2041 a value of type `T` itself. This ensures that the function works with
2042 the broadest set of types possible, since some types are expensive or
2043 illegal to copy and pass by value.
2044
2045 Generic `type`, `struct`, and `enum` declarations follow the same pattern:
2046
2047 ~~~~
2048 extern crate collections;
2049 type Set<T> = collections::HashMap<T, ()>;
2050
2051 struct Stack<T> {
2052     elements: ~[T]
2053 }
2054
2055 enum Option<T> {
2056     Some(T),
2057     None
2058 }
2059 # fn main() {}
2060 ~~~~
2061
2062 These declarations can be instantiated to valid types like `Set<int>`,
2063 `Stack<int>`, and `Option<int>`.
2064
2065 The last type in that example, `Option`, appears frequently in Rust code.
2066 Because Rust does not have null pointers (except in unsafe code), we need
2067 another way to write a function whose result isn't defined on every possible
2068 combination of arguments of the appropriate types. The usual way is to write
2069 a function that returns `Option<T>` instead of `T`.
2070
2071 ~~~~
2072 # struct Point { x: f64, y: f64 }
2073 # enum Shape { Circle(Point, f64), Rectangle(Point, Point) }
2074 fn radius(shape: Shape) -> Option<f64> {
2075     match shape {
2076         Circle(_, radius) => Some(radius),
2077         Rectangle(..)     => None
2078     }
2079 }
2080 ~~~~
2081
2082 The Rust compiler compiles generic functions very efficiently by
2083 *monomorphizing* them. *Monomorphization* is a fancy name for a simple
2084 idea: generate a separate copy of each generic function at each call site,
2085 a copy that is specialized to the argument
2086 types and can thus be optimized specifically for them. In this
2087 respect, Rust's generics have similar performance characteristics to
2088 C++ templates.
2089
2090 ## Traits
2091
2092 Within a generic function—that is, a function parameterized by a
2093 type parameter, say, `T`—the operations we can do on arguments of
2094 type `T` are quite limited.  After all, since we don't know what type
2095 `T` will be instantiated with, we can't safely modify or query values
2096 of type `T`.  This is where _traits_ come into play. Traits are Rust's
2097 most powerful tool for writing polymorphic code. Java developers will
2098 see them as similar to Java interfaces, and Haskellers will notice
2099 their similarities to type classes. Rust's traits give us a way to
2100 express *bounded polymorphism*: by limiting the set of possible types
2101 that a type parameter could refer to, they expand the number of
2102 operations we can safely perform on arguments of that type.
2103
2104 As motivation, let us consider copying of values in Rust.  The `clone`
2105 method is not defined for values of every type.  One reason is
2106 user-defined destructors: copying a value of a type that has a
2107 destructor could result in the destructor running multiple times.
2108 Therefore, values of types that have destructors cannot be copied
2109 unless we explicitly implement `clone` for them.
2110
2111 This complicates handling of generic functions.
2112 If we have a function with a type parameter `T`,
2113 can we copy values of type `T` inside that function?
2114 In Rust, we can't,
2115 and if we try to run the following code the compiler will complain.
2116
2117 ~~~~ {.ignore}
2118 // This does not compile
2119 fn head_bad<T>(v: &[T]) -> T {
2120     v[0] // error: copying a non-copyable value
2121 }
2122 ~~~~
2123
2124 However, we can tell the compiler
2125 that the `head` function is only for copyable types.
2126 In Rust, copyable types are those that _implement the `Clone` trait_.
2127 We can then explicitly create a second copy of the value we are returning
2128 by calling the `clone` method:
2129
2130 ~~~~
2131 // This does
2132 fn head<T: Clone>(v: &[T]) -> T {
2133     v[0].clone()
2134 }
2135 ~~~~
2136
2137 The bounded type parameter `T: Clone` says that `head`
2138 can be called on an argument of type `&[T]` for any `T`,
2139 so long as there is an implementation of the
2140 `Clone` trait for `T`.
2141 When instantiating a generic function,
2142 we can only instantiate it with types
2143 that implement the correct trait,
2144 so we could not apply `head` to a vector whose elements are of some type
2145 that does not implement `Clone`.
2146
2147 While most traits can be defined and implemented by user code,
2148 three traits are automatically derived and implemented
2149 for all applicable types by the compiler,
2150 and may not be overridden:
2151
2152 * `Send` - Sendable types.
2153 Types are sendable
2154 unless they contain references.
2155
2156 * `Share` - Types that are *threadsafe*
2157 These are types that are safe to be used across several threads with access to
2158 a `&T` pointer. `Mutex<T>` is an example of a *sharable* type with internal mutable data.
2159
2160 * `'static` - Non-borrowed types.
2161 These are types that do not contain any data whose lifetime is bound to
2162 a particular stack frame. These are types that do not contain any
2163 references, or types where the only contained references
2164 have the `'static` lifetime. (For more on named lifetimes and their uses,
2165 see the [references and lifetimes guide][lifetimes].)
2166
2167 > *Note:* These built-in traits were referred to as 'kinds' in earlier
2168 > iterations of the language, and often still are.
2169
2170 Additionally, the `Drop` trait is used to define destructors. This
2171 trait provides one method called `drop`, which is automatically
2172 called when a value of the type that implements this trait is
2173 destroyed, either because the value went out of scope or because the
2174 garbage collector reclaimed it.
2175
2176 ~~~
2177 struct TimeBomb {
2178     explosivity: uint
2179 }
2180
2181 impl Drop for TimeBomb {
2182     fn drop(&mut self) {
2183         for _ in range(0, self.explosivity) {
2184             println!("blam!");
2185         }
2186     }
2187 }
2188 ~~~
2189
2190 It is illegal to call `drop` directly. Only code inserted by the compiler
2191 may call it.
2192
2193 ## Declaring and implementing traits
2194
2195 At its simplest, a trait is a set of zero or more _method signatures_.
2196 For example, we could declare the trait
2197 `Printable` for things that can be printed to the console,
2198 with a single method signature:
2199
2200 ~~~~
2201 trait Printable {
2202     fn print(&self);
2203 }
2204 ~~~~
2205
2206 We say that the `Printable` trait _provides_ a `print` method with the
2207 given signature.  This means that we can call `print` on an argument
2208 of any type that implements the `Printable` trait.
2209
2210 Rust's built-in `Send` and `Share` types are examples of traits that
2211 don't provide any methods.
2212
2213 Traits may be implemented for specific types with [impls]. An impl for
2214 a particular trait gives an implementation of the methods that
2215 trait provides.  For instance, the following impls of
2216 `Printable` for `int` and `StrBuf` give implementations of the `print`
2217 method.
2218
2219 [impls]: #methods
2220
2221 ~~~~
2222 # trait Printable { fn print(&self); }
2223 impl Printable for int {
2224     fn print(&self) { println!("{:?}", *self) }
2225 }
2226
2227 impl Printable for StrBuf {
2228     fn print(&self) { println!("{}", *self) }
2229 }
2230
2231 # 1.print();
2232 # ("foo".to_strbuf()).print();
2233 ~~~~
2234
2235 Methods defined in an impl for a trait may be called just like
2236 any other method, using dot notation, as in `1.print()`.
2237
2238 ## Default method implementations in trait definitions
2239
2240 Sometimes, a method that a trait provides will have the same
2241 implementation for most or all of the types that implement that trait.
2242 For instance, suppose that we wanted `bool`s and `f32`s to be
2243 printable, and that we wanted the implementation of `print` for those
2244 types to be exactly as it is for `int`, above:
2245
2246 ~~~~
2247 # trait Printable { fn print(&self); }
2248 impl Printable for f32 {
2249     fn print(&self) { println!("{:?}", *self) }
2250 }
2251
2252 impl Printable for bool {
2253     fn print(&self) { println!("{:?}", *self) }
2254 }
2255
2256 # true.print();
2257 # 3.14159.print();
2258 ~~~~
2259
2260 This works fine, but we've now repeated the same definition of `print`
2261 in three places.  Instead of doing that, we can simply include the
2262 definition of `print` right in the trait definition, instead of just
2263 giving its signature.  That is, we can write the following:
2264
2265 ~~~~
2266 trait Printable {
2267         // Default method implementation
2268     fn print(&self) { println!("{:?}", *self) }
2269 }
2270
2271 impl Printable for int {}
2272
2273 impl Printable for StrBuf {
2274     fn print(&self) { println!("{}", *self) }
2275 }
2276
2277 impl Printable for bool {}
2278
2279 impl Printable for f32 {}
2280
2281 # 1.print();
2282 # ("foo".to_strbuf()).print();
2283 # true.print();
2284 # 3.14159.print();
2285 ~~~~
2286
2287 Here, the impls of `Printable` for `int`, `bool`, and `f32` don't
2288 need to provide an implementation of `print`, because in the absence
2289 of a specific implementation, Rust just uses the _default method_
2290 provided in the trait definition.  Depending on the trait, default
2291 methods can save a great deal of boilerplate code from having to be
2292 written in impls.  Of course, individual impls can still override the
2293 default method for `print`, as is being done above in the impl for
2294 `StrBuf`.
2295
2296 ## Type-parameterized traits
2297
2298 Traits may be parameterized by type variables.  For example, a trait
2299 for generalized sequence types might look like the following:
2300
2301 ~~~~
2302 trait Seq<T> {
2303     fn length(&self) -> uint;
2304 }
2305
2306 impl<T> Seq<T> for ~[T] {
2307     fn length(&self) -> uint { self.len() }
2308 }
2309 ~~~~
2310
2311 The implementation has to explicitly declare the type parameter that
2312 it binds, `T`, before using it to specify its trait type. Rust
2313 requires this declaration because the `impl` could also, for example,
2314 specify an implementation of `Seq<int>`. The trait type (appearing
2315 between `impl` and `for`) *refers* to a type, rather than
2316 defining one.
2317
2318 The type parameters bound by a trait are in scope in each of the
2319 method declarations. So, re-declaring the type parameter
2320 `T` as an explicit type parameter for `length`, in either the trait or
2321 the impl, would be a compile-time error.
2322
2323 Within a trait definition, `Self` is a special type that you can think
2324 of as a type parameter. An implementation of the trait for any given
2325 type `T` replaces the `Self` type parameter with `T`. The following
2326 trait describes types that support an equality operation:
2327
2328 ~~~~
2329 // In a trait, `self` refers to the self argument.
2330 // `Self` refers to the type implementing the trait.
2331 trait Eq {
2332     fn equals(&self, other: &Self) -> bool;
2333 }
2334
2335 // In an impl, `self` refers just to the value of the receiver
2336 impl Eq for int {
2337     fn equals(&self, other: &int) -> bool { *other == *self }
2338 }
2339 ~~~~
2340
2341 Notice that in the trait definition, `equals` takes a
2342 second parameter of type `Self`.
2343 In contrast, in the `impl`, `equals` takes a second parameter of
2344 type `int`, only using `self` as the name of the receiver.
2345
2346 Just as in type implementations, traits can define standalone (static)
2347 methods.  These methods are called by prefixing the method name with the trait
2348 name and a double colon.  The compiler uses type inference to decide which
2349 implementation to use.
2350
2351 ~~~~
2352 use std::f64::consts::PI;
2353 trait Shape { fn new(area: f64) -> Self; }
2354 struct Circle { radius: f64 }
2355 struct Square { length: f64 }
2356
2357 impl Shape for Circle {
2358     fn new(area: f64) -> Circle { Circle { radius: (area / PI).sqrt() } }
2359 }
2360 impl Shape for Square {
2361     fn new(area: f64) -> Square { Square { length: area.sqrt() } }
2362 }
2363
2364 let area = 42.5;
2365 let c: Circle = Shape::new(area);
2366 let s: Square = Shape::new(area);
2367 ~~~~
2368
2369 ## Bounded type parameters and static method dispatch
2370
2371 Traits give us a language for defining predicates on types, or
2372 abstract properties that types can have. We can use this language to
2373 define _bounds_ on type parameters, so that we can then operate on
2374 generic types.
2375
2376 ~~~~
2377 # trait Printable { fn print(&self); }
2378 fn print_all<T: Printable>(printable_things: ~[T]) {
2379     for thing in printable_things.iter() {
2380         thing.print();
2381     }
2382 }
2383 ~~~~
2384
2385 Declaring `T` as conforming to the `Printable` trait (as we earlier
2386 did with `Clone`) makes it possible to call methods from that trait
2387 on values of type `T` inside the function. It will also cause a
2388 compile-time error when anyone tries to call `print_all` on a vector
2389 whose element type does not have a `Printable` implementation.
2390
2391 Type parameters can have multiple bounds by separating them with `+`,
2392 as in this version of `print_all` that copies elements.
2393
2394 ~~~
2395 # trait Printable { fn print(&self); }
2396 fn print_all<T: Printable + Clone>(printable_things: ~[T]) {
2397     let mut i = 0;
2398     while i < printable_things.len() {
2399         let copy_of_thing = printable_things[i].clone();
2400         copy_of_thing.print();
2401         i += 1;
2402     }
2403 }
2404 ~~~
2405
2406 Method calls to bounded type parameters are _statically dispatched_,
2407 imposing no more overhead than normal function invocation, so are
2408 the preferred way to use traits polymorphically.
2409
2410 This usage of traits is similar to Haskell type classes.
2411
2412 ## Trait objects and dynamic method dispatch
2413
2414 The above allows us to define functions that polymorphically act on
2415 values of a single unknown type that conforms to a given trait.
2416 However, consider this function:
2417
2418 ~~~~
2419 # type Circle = int; type Rectangle = int;
2420 # impl Drawable for int { fn draw(&self) {} }
2421 # fn new_circle() -> int { 1 }
2422 trait Drawable { fn draw(&self); }
2423
2424 fn draw_all<T: Drawable>(shapes: ~[T]) {
2425     for shape in shapes.iter() { shape.draw(); }
2426 }
2427 # let c: Circle = new_circle();
2428 # draw_all(~[c]);
2429 ~~~~
2430
2431 You can call that on a vector of circles, or a vector of rectangles
2432 (assuming those have suitable `Drawable` traits defined), but not on
2433 a vector containing both circles and rectangles. When such behavior is
2434 needed, a trait name can alternately be used as a type, called
2435 an _object_.
2436
2437 ~~~~
2438 # trait Drawable { fn draw(&self); }
2439 fn draw_all(shapes: &[Box<Drawable>]) {
2440     for shape in shapes.iter() { shape.draw(); }
2441 }
2442 ~~~~
2443
2444 In this example, there is no type parameter. Instead, the `~Drawable`
2445 type denotes any owned box value that implements the `Drawable` trait.
2446 To construct such a value, you use the `as` operator to cast a value
2447 to an object:
2448
2449 ~~~~
2450 # type Circle = int; type Rectangle = bool;
2451 # trait Drawable { fn draw(&self); }
2452 # fn new_circle() -> Circle { 1 }
2453 # fn new_rectangle() -> Rectangle { true }
2454 # fn draw_all(shapes: &[Box<Drawable>]) {}
2455
2456 impl Drawable for Circle { fn draw(&self) { /* ... */ } }
2457 impl Drawable for Rectangle { fn draw(&self) { /* ... */ } }
2458
2459 let c: Box<Circle> = box new_circle();
2460 let r: Box<Rectangle> = box new_rectangle();
2461 draw_all([c as Box<Drawable>, r as Box<Drawable>]);
2462 ~~~~
2463
2464 We omit the code for `new_circle` and `new_rectangle`; imagine that
2465 these just return `Circle`s and `Rectangle`s with a default size. Note
2466 that, like strings and vectors, objects have dynamic size and may
2467 only be referred to via one of the pointer types.
2468 Other pointer types work as well.
2469 Casts to traits may only be done with compatible pointers so,
2470 for example, an `&Circle` may not be cast to a `Box<Drawable>`.
2471
2472 ~~~
2473 # type Circle = int; type Rectangle = int;
2474 # trait Drawable { fn draw(&self); }
2475 # impl Drawable for int { fn draw(&self) {} }
2476 # fn new_circle() -> int { 1 }
2477 # fn new_rectangle() -> int { 2 }
2478 // An owned object
2479 let owny: Box<Drawable> = box new_circle() as Box<Drawable>;
2480 // A borrowed object
2481 let stacky: &Drawable = &new_circle() as &Drawable;
2482 ~~~
2483
2484 Method calls to trait types are _dynamically dispatched_. Since the
2485 compiler doesn't know specifically which functions to call at compile
2486 time, it uses a lookup table (also known as a vtable or dictionary) to
2487 select the method to call at runtime.
2488
2489 This usage of traits is similar to Java interfaces.
2490
2491 There are some built-in bounds, such as `Send` and `Share`, which are properties
2492 of the components of types. By design, trait objects don't know the exact type
2493 of their contents and so the compiler cannot reason about those properties.
2494
2495 You can instruct the compiler, however, that the contents of a trait object must
2496 acribe to a particular bound with a trailing colon (`:`). These are examples of
2497 valid types:
2498
2499 ~~~rust
2500 trait Foo {}
2501 trait Bar<T> {}
2502
2503 fn sendable_foo(f: Box<Foo:Send>) { /* ... */ }
2504 fn shareable_bar<T: Share>(b: &Bar<T>: Share) { /* ... */ }
2505 ~~~
2506
2507 When no colon is specified (such as the type `~Foo`), it is inferred that the
2508 value ascribes to no bounds. They must be added manually if any bounds are
2509 necessary for usage.
2510
2511 Builtin kind bounds can also be specified on closure types in the same way (for
2512 example, by writing `fn:Send()`), and the default behaviours are the same as
2513 for traits of the same storage class.
2514
2515 ## Trait inheritance
2516
2517 We can write a trait declaration that _inherits_ from other traits, called _supertraits_.
2518 Types that implement a trait must also implement its supertraits.
2519 For example,
2520 we can define a `Circle` trait that inherits from `Shape`.
2521
2522 ~~~~
2523 trait Shape { fn area(&self) -> f64; }
2524 trait Circle : Shape { fn radius(&self) -> f64; }
2525 ~~~~
2526
2527 Now, we can implement `Circle` on a type only if we also implement `Shape`.
2528
2529 ~~~~
2530 use std::f64::consts::PI;
2531 # trait Shape { fn area(&self) -> f64; }
2532 # trait Circle : Shape { fn radius(&self) -> f64; }
2533 # struct Point { x: f64, y: f64 }
2534 # fn square(x: f64) -> f64 { x * x }
2535 struct CircleStruct { center: Point, radius: f64 }
2536 impl Circle for CircleStruct {
2537     fn radius(&self) -> f64 { (self.area() / PI).sqrt() }
2538 }
2539 impl Shape for CircleStruct {
2540     fn area(&self) -> f64 { PI * square(self.radius) }
2541 }
2542 ~~~~
2543
2544 Notice that methods of `Circle` can call methods on `Shape`, as our
2545 `radius` implementation calls the `area` method.
2546 This is a silly way to compute the radius of a circle
2547 (since we could just return the `radius` field), but you get the idea.
2548
2549 In type-parameterized functions,
2550 methods of the supertrait may be called on values of subtrait-bound type parameters.
2551 Refering to the previous example of `trait Circle : Shape`:
2552
2553 ~~~
2554 # trait Shape { fn area(&self) -> f64; }
2555 # trait Circle : Shape { fn radius(&self) -> f64; }
2556 fn radius_times_area<T: Circle>(c: T) -> f64 {
2557     // `c` is both a Circle and a Shape
2558     c.radius() * c.area()
2559 }
2560 ~~~
2561
2562 Likewise, supertrait methods may also be called on trait objects.
2563
2564 ~~~ {.ignore}
2565 use std::f64::consts::PI;
2566 # trait Shape { fn area(&self) -> f64; }
2567 # trait Circle : Shape { fn radius(&self) -> f64; }
2568 # struct Point { x: f64, y: f64 }
2569 # struct CircleStruct { center: Point, radius: f64 }
2570 # impl Circle for CircleStruct { fn radius(&self) -> f64 { (self.area() / PI).sqrt() } }
2571 # impl Shape for CircleStruct { fn area(&self) -> f64 { PI * square(self.radius) } }
2572
2573 let concrete = ~CircleStruct{center:Point{x:3.0,y:4.0},radius:5.0};
2574 let mycircle: ~Circle = concrete as ~Circle;
2575 let nonsense = mycircle.radius() * mycircle.area();
2576 ~~~
2577
2578 > *Note:* Trait inheritance does not actually work with objects yet
2579
2580 ## Deriving implementations for traits
2581
2582 A small number of traits in `std` and `extra` can have implementations
2583 that can be automatically derived. These instances are specified by
2584 placing the `deriving` attribute on a data type declaration. For
2585 example, the following will mean that `Circle` has an implementation
2586 for `Eq` and can be used with the equality operators, and that a value
2587 of type `ABC` can be randomly generated and converted to a string:
2588
2589 ~~~
2590 extern crate rand;
2591
2592 #[deriving(Eq)]
2593 struct Circle { radius: f64 }
2594
2595 #[deriving(Rand, Show)]
2596 enum ABC { A, B, C }
2597
2598 fn main() {
2599     // Use the Show trait to print "A, B, C."
2600     println!("{}, {}, {}", A, B, C);
2601 }
2602 ~~~
2603
2604 The full list of derivable traits is `Eq`, `TotalEq`, `Ord`,
2605 `TotalOrd`, `Encodable`, `Decodable`, `Clone`,
2606 `Hash`, `Rand`, `Default`, `Zero`, `FromPrimitive` and `Show`.
2607
2608 # Crates and the module system
2609
2610 Rust's module system is very powerful, but because of that also somewhat complex.
2611 Nevertheless, this section will try to explain every important aspect of it.
2612
2613 ## Crates
2614
2615 In order to speak about the module system, we first need to define the medium it exists in:
2616
2617 Let's say you've written a program or a library, compiled it, and got the resulting binary.
2618 In Rust, the content of all source code that the compiler directly had to compile in order to end up with
2619 that binary is collectively called a 'crate'.
2620
2621 For example, for a simple hello world program your crate only consists of this code:
2622
2623 ~~~~
2624 // `main.rs`
2625 fn main() {
2626     println!("Hello world!");
2627 }
2628 ~~~~
2629
2630 A crate is also the unit of independent compilation in Rust: `rustc` always compiles a single crate at a time,
2631 from which it produces either a library or an executable.
2632
2633 Note that merely using an already compiled library in your code does not make it part of your crate.
2634
2635 ## The module hierarchy
2636
2637 For every crate, all the code in it is arranged in a hierarchy of modules starting with a single
2638 root module. That root module is called the 'crate root'.
2639
2640 All modules in a crate below the crate root are declared with the `mod` keyword:
2641
2642 ~~~~
2643 // This is the crate root
2644
2645 mod farm {
2646     // This is the body of module 'farm' declared in the crate root.
2647
2648     fn chicken() { println!("cluck cluck"); }
2649     fn cow() { println!("mooo"); }
2650
2651     mod barn {
2652         // Body of module 'barn'
2653
2654         fn hay() { println!("..."); }
2655     }
2656 }
2657
2658 fn main() {
2659     println!("Hello farm!");
2660 }
2661 ~~~~
2662
2663 As you can see, your module hierarchy is now three modules deep: There is the crate root, which contains your `main()`
2664 function, and the module `farm`. The module `farm` also contains two functions and a third module `barn`,
2665 which contains a function `hay`.
2666
2667 ## Paths and visibility
2668
2669 We've now defined a nice module hierarchy. But how do we access the items in it from our `main` function?
2670 One way to do it is to simply fully qualifying it:
2671
2672 ~~~~ {.ignore}
2673 mod farm {
2674     fn chicken() { println!("cluck cluck"); }
2675     // ...
2676 }
2677
2678 fn main() {
2679     println!("Hello chicken!");
2680
2681     ::farm::chicken(); // Won't compile yet, see further down
2682 }
2683 ~~~~
2684
2685 The `::farm::chicken` construct is what we call a 'path'.
2686
2687 Because it's starting with a `::`, it's also a 'global path', which qualifies
2688 an item by its full path in the module hierarchy relative to the crate root.
2689
2690 If the path were to start with a regular identifier, like `farm::chicken`, it
2691 would be a 'local path' instead. We'll get to them later.
2692
2693 Now, if you actually tried to compile this code example, you'll notice that you
2694 get a `function 'chicken' is private` error. That's because by default, items
2695 (`fn`, `struct`, `static`, `mod`, ...) are private.
2696
2697 To make them visible outside their containing modules, you need to mark them
2698 _public_ with `pub`:
2699
2700 ~~~~
2701 mod farm {
2702     pub fn chicken() { println!("cluck cluck"); }
2703     pub fn cow() { println!("mooo"); }
2704     // ...
2705 }
2706
2707 fn main() {
2708     println!("Hello chicken!");
2709     ::farm::chicken(); // This compiles now
2710 }
2711 ~~~~
2712
2713 Visibility restrictions in Rust exist only at module boundaries. This
2714 is quite different from most object-oriented languages that also
2715 enforce restrictions on objects themselves. That's not to say that
2716 Rust doesn't support encapsulation: both struct fields and methods can
2717 be private. But this encapsulation is at the module level, not the
2718 struct level.
2719
2720 Fields are _private_ by default, and can be made _public_ with
2721 the `pub` keyword:
2722
2723 ~~~
2724 mod farm {
2725 # pub type Chicken = int;
2726 # struct Human(int);
2727 # impl Human { pub fn rest(&self) { } }
2728 # pub fn make_me_a_farm() -> Farm { Farm { chickens: ~[], farmer: Human(0) } }
2729     pub struct Farm {
2730         chickens: ~[Chicken],
2731         pub farmer: Human
2732     }
2733
2734     impl Farm {
2735         fn feed_chickens(&self) { /* ... */ }
2736         pub fn add_chicken(&self, c: Chicken) { /* ... */ }
2737     }
2738
2739     pub fn feed_animals(farm: &Farm) {
2740         farm.feed_chickens();
2741     }
2742 }
2743
2744 fn main() {
2745     let f = make_me_a_farm();
2746     f.add_chicken(make_me_a_chicken());
2747     farm::feed_animals(&f);
2748     f.farmer.rest();
2749
2750     // This wouldn't compile because both are private:
2751     // `f.feed_chickens();`
2752     // `let chicken_counter = f.chickens.len();`
2753 }
2754 # fn make_me_a_farm() -> farm::Farm { farm::make_me_a_farm() }
2755 # fn make_me_a_chicken() -> farm::Chicken { 0 }
2756 ~~~
2757
2758 Exact details and specifications about visibility rules can be found in the Rust
2759 manual.
2760
2761 ## Files and modules
2762
2763 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.
2764
2765 The only file that's relevant when compiling is the one that contains the body
2766 of your crate root, and it's only relevant because you have to pass that file
2767 to `rustc` to compile your crate.
2768
2769 In principle, that's all you need: You can write any Rust program as one giant source file that contains your
2770 crate root and everything else in `mod ... { ... }` declarations.
2771
2772 However, in practice you usually want to split up your code into multiple
2773 source files to make it more manageable. Rust allows you to move the body of
2774 any module into its own source file. If you declare a module without its body,
2775 like `mod foo;`, the compiler will look for the files `foo.rs` and `foo/mod.rs`
2776 inside some directory (usually the same as of the source file containing the
2777 `mod foo;` declaration). If it finds either, it uses the content of that file
2778 as the body of the module. If it finds both, that's a compile error.
2779
2780 To move the content of `mod farm` into its own file, you can write:
2781
2782 ~~~~ {.ignore}
2783 // `main.rs` - contains body of the crate root
2784 mod farm; // Compiler will look for `farm.rs` and `farm/mod.rs`
2785
2786 fn main() {
2787     println!("Hello farm!");
2788     ::farm::cow();
2789 }
2790 ~~~~
2791
2792 ~~~~
2793 // `farm.rs` - contains body of module 'farm' in the crate root
2794 pub fn chicken() { println!("cluck cluck"); }
2795 pub fn cow() { println!("mooo"); }
2796
2797 pub mod barn {
2798     pub fn hay() { println!("..."); }
2799 }
2800 # fn main() { }
2801 ~~~~
2802
2803 In short, `mod foo;` is just syntactic sugar for `mod foo { /* content of <...>/foo.rs or <...>/foo/mod.rs */ }`.
2804
2805 This also means that having two or more identical `mod foo;` declarations
2806 somewhere in your crate hierarchy is generally a bad idea,
2807 just like copy-and-paste-ing a module into multiple places is a bad idea.
2808 Both will result in duplicate and mutually incompatible definitions.
2809
2810 When `rustc` resolves these module declarations, it starts by looking in the
2811 parent directory of the file containing the `mod foo` declaration. For example,
2812 given a file with the module body:
2813
2814 ~~~ {.ignore}
2815 // `src/main.rs`
2816 mod plants;
2817 mod animals {
2818     mod fish;
2819     mod mammals {
2820         mod humans;
2821     }
2822 }
2823 ~~~
2824
2825 The compiler will look for these files, in this order:
2826
2827 ~~~ {.notrust}
2828 src/plants.rs
2829 src/plants/mod.rs
2830
2831 src/animals/fish.rs
2832 src/animals/fish/mod.rs
2833
2834 src/animals/mammals/humans.rs
2835 src/animals/mammals/humans/mod.rs
2836 ~~~
2837
2838 Keep in mind that identical module hierarchies can still lead to different path
2839 lookups depending on how and where you've moved a module body to its own file.
2840 For example, if you move the `animals` module into its own file:
2841
2842 ~~~ {.ignore}
2843 // `src/main.rs`
2844 mod plants;
2845 mod animals;
2846 ~~~
2847
2848 ~~~ {.ignore}
2849 // `src/animals.rs` or `src/animals/mod.rs`
2850 mod fish;
2851 mod mammals {
2852     mod humans;
2853 }
2854 ~~~
2855
2856 ...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:
2857
2858 ~~~ {.notrust}
2859 src/animals.rs
2860     src/fish.rs
2861     src/fish/mod.rs
2862
2863     src/mammals/humans.rs
2864     src/mammals/humans/mod.rs
2865 ~~~
2866
2867 If the animals file is `src/animals/mod.rs`, `rustc` will look for:
2868
2869 ~~~ {.notrust}
2870 src/animals/mod.rs
2871     src/animals/fish.rs
2872     src/animals/fish/mod.rs
2873
2874     src/animals/mammals/humans.rs
2875     src/animals/mammals/humans/mod.rs
2876
2877 ~~~
2878
2879 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.
2880
2881 If you need to override where `rustc` will look for the file containing a
2882 module's source code, use the `path` compiler directive. For example, to load a
2883 `classified` module from a different file:
2884
2885 ~~~ {.ignore}
2886 #[path="../../area51/alien.rs"]
2887 mod classified;
2888 ~~~
2889
2890 ## Importing names into the local scope
2891
2892 Always referring to definitions in other modules with their global
2893 path gets old really fast, so Rust has a way to import
2894 them into the local scope of your module: `use`-statements.
2895
2896 They work like this: At the beginning of any module body, `fn` body, or any other block
2897 you can write a list of `use`-statements, consisting of the keyword `use` and a __global path__ to an item
2898 without the `::` prefix. For example, this imports `cow` into the local scope:
2899
2900 ~~~
2901 use farm::cow;
2902 # mod farm { pub fn cow() { println!("I'm a hidden ninja cow!") } }
2903 # fn main() { cow() }
2904 ~~~
2905
2906 The path you give to `use` is per default global, meaning relative to the crate root,
2907 no matter how deep the module hierarchy is, or whether the module body it's written in
2908 is contained in its own file. (Remember: files are irrelevant.)
2909
2910 This is different from other languages, where you often only find a single import construct that combines the semantic
2911 of `mod foo;` and `use`-statements, and which tend to work relative to the source file or use an absolute file path
2912 - Ruby's `require` or C/C++'s `#include` come to mind.
2913
2914 However, it's also possible to import things relative to the module of the `use`-statement:
2915 Adding a `super::` in front of the path will start in the parent module,
2916 while adding a `self::` prefix will start in the current module:
2917
2918 ~~~
2919 # mod workaround {
2920 # pub fn some_parent_item(){ println!("...") }
2921 # mod foo {
2922 use super::some_parent_item;
2923 use self::some_child_module::some_item;
2924 # pub fn bar() { some_parent_item(); some_item() }
2925 # pub mod some_child_module { pub fn some_item() {} }
2926 # }
2927 # }
2928 ~~~
2929
2930 Again - relative to the module, not to the file.
2931
2932 Imports are also shadowed by local definitions:
2933 For each name you mention in a module/block, `rust`
2934 will first look at all items that are defined locally,
2935 and only if that results in no match look at items you brought in
2936 scope with corresponding `use` statements.
2937
2938 ~~~ {.ignore}
2939 # // FIXME: Allow unused import in doc test
2940 use farm::cow;
2941 // ...
2942 # mod farm { pub fn cow() { println!("Hidden ninja cow is hidden.") } }
2943 fn cow() { println!("Mooo!") }
2944
2945 fn main() {
2946     cow() // resolves to the locally defined `cow()` function
2947 }
2948 ~~~
2949
2950 To make this behavior more obvious, the rule has been made that `use`-statement always need to be written
2951 before any declaration, like in the example above. This is a purely artificial rule introduced
2952 because people always assumed they shadowed each other based on order, despite the fact that all items in rust are
2953 mutually recursive, order independent definitions.
2954
2955 One odd consequence of that rule is that `use` statements also go in front of any `mod` declaration,
2956 even if they refer to things inside them:
2957
2958 ~~~
2959 use farm::cow;
2960 mod farm {
2961     pub fn cow() { println!("Moooooo?") }
2962 }
2963
2964 fn main() { cow() }
2965 ~~~
2966
2967 This is what our `farm` example looks like with `use` statements:
2968
2969 ~~~~
2970 use farm::chicken;
2971 use farm::cow;
2972 use farm::barn;
2973
2974 mod farm {
2975     pub fn chicken() { println!("cluck cluck"); }
2976     pub fn cow() { println!("mooo"); }
2977
2978     pub mod barn {
2979         pub fn hay() { println!("..."); }
2980     }
2981 }
2982
2983 fn main() {
2984     println!("Hello farm!");
2985
2986     // Can now refer to those names directly:
2987     chicken();
2988     cow();
2989     barn::hay();
2990 }
2991 ~~~~
2992
2993 And here an example with multiple files:
2994
2995 ~~~{.ignore}
2996 // `a.rs` - crate root
2997 use b::foo;
2998 use b::c::bar;
2999 mod b;
3000 fn main() {
3001     foo();
3002     bar();
3003 }
3004 ~~~
3005
3006 ~~~{.ignore}
3007 // `b/mod.rs`
3008 pub mod c;
3009 pub fn foo() { println!("Foo!"; }
3010 ~~~
3011
3012 ~~~{.ignore}
3013 // `b/c.rs`
3014 pub fn bar() { println!("Bar!"); }
3015 ~~~
3016
3017 There also exist two short forms for importing multiple names at once:
3018
3019 1. Explicit mention multiple names as the last element of an `use` path:
3020
3021 ~~~
3022 use farm::{chicken, cow};
3023 # mod farm {
3024 #     pub fn cow() { println!("Did I already mention how hidden and ninja I am?") }
3025 #     pub fn chicken() { println!("I'm Bat-chicken, guardian of the hidden tutorial code.") }
3026 # }
3027 # fn main() { cow(); chicken() }
3028 ~~~
3029
3030 2. Import everything in a module with a wildcard:
3031
3032 ~~~
3033 use farm::*;
3034 # mod farm {
3035 #     pub fn cow() { println!("Bat-chicken? What a stupid name!") }
3036 #     pub fn chicken() { println!("Says the 'hidden ninja' cow.") }
3037 # }
3038 # fn main() { cow(); chicken() }
3039 ~~~
3040
3041 > *Note:* This feature of the compiler is currently gated behind the
3042 > `#![feature(globs)]` directive. More about these directives can be found in
3043 > the manual.
3044
3045 However, that's not all. You can also rename an item while you're bringing it into scope:
3046
3047 ~~~
3048 use egg_layer = farm::chicken;
3049 # mod farm { pub fn chicken() { println!("Laying eggs is fun!")  } }
3050 // ...
3051
3052 fn main() {
3053     egg_layer();
3054 }
3055 ~~~
3056
3057 In general, `use` creates a local alias:
3058 An alternate path and a possibly different name to access the same item,
3059 without touching the original, and with both being interchangeable.
3060
3061 ## Reexporting names
3062
3063 It is also possible to reexport items to be accessible under your module.
3064
3065 For that, you write `pub use`:
3066
3067 ~~~
3068 mod farm {
3069     pub use self::barn::hay;
3070
3071     pub fn chicken() { println!("cluck cluck"); }
3072     pub fn cow() { println!("mooo"); }
3073
3074     mod barn {
3075         pub fn hay() { println!("..."); }
3076     }
3077 }
3078
3079 fn main() {
3080     farm::chicken();
3081     farm::cow();
3082     farm::hay();
3083 }
3084 ~~~
3085
3086 Just like in normal `use` statements, the exported names
3087 merely represent an alias to the same thing and can also be renamed.
3088
3089 The above example also demonstrate what you can use `pub use` for:
3090 The nested `barn` module is private, but the `pub use` allows users
3091 of the module `farm` to access a function from `barn` without needing
3092 to know that `barn` exists.
3093
3094 In other words, you can use it to decouple a public api from its internal implementation.
3095
3096 ## Using libraries
3097
3098 So far we've only talked about how to define and structure your own crate.
3099
3100 However, most code out there will want to use preexisting libraries,
3101 as there really is no reason to start from scratch each time you start a new project.
3102
3103 In Rust terminology, we need a way to refer to other crates.
3104
3105 For that, Rust offers you the `extern crate` declaration:
3106
3107 ~~~
3108 extern crate num;
3109 // `num` ships with Rust (much like `extra`; more details further down).
3110
3111 fn main() {
3112     // The rational number '1/2':
3113     let one_half = ::num::rational::Ratio::new(1, 2);
3114 }
3115 ~~~
3116
3117 A statement of the form `extern crate foo;` will cause `rustc` to search for the crate `foo`,
3118 and if it finds a matching binary it lets you use it from inside your crate.
3119
3120 The effect it has on your module hierarchy mirrors aspects of both `mod` and `use`:
3121
3122 - Like `mod`, it causes `rustc` to actually emit code:
3123   The linkage information the binary needs to use the library `foo`.
3124
3125 - But like `use`, all `extern crate` statements that refer to the same library are interchangeable,
3126   as each one really just presents an alias to an external module (the crate root of the library
3127   you're linking against).
3128
3129 Remember how `use`-statements have to go before local declarations because the latter shadows the former?
3130 Well, `extern crate` statements also have their own rules in that regard:
3131 Both `use` and local declarations can shadow them, so the rule is that `extern crate` has to go in front
3132 of both `use` and local declarations.
3133
3134 Which can result in something like this:
3135
3136 ~~~
3137 extern crate num;
3138
3139 use farm::dog;
3140 use num::rational::Ratio;
3141
3142 mod farm {
3143     pub fn dog() { println!("woof"); }
3144 }
3145
3146 fn main() {
3147     farm::dog();
3148     let a_third = Ratio::new(1, 3);
3149 }
3150 ~~~
3151
3152 It's a bit weird, but it's the result of shadowing rules that have been set that way because
3153 they model most closely what people expect to shadow.
3154
3155 ## Crate metadata and settings
3156
3157 For every crate you can define a number of metadata items, such as link name, version or author.
3158 You can also toggle settings that have crate-global consequences. Both mechanism
3159 work by providing attributes in the crate root.
3160
3161 For example, Rust uniquely identifies crates by their link metadata, which includes
3162 the link name and the version. It also hashes the filename and the symbols in a binary
3163 based on the link metadata, allowing you to use two different versions of the same library in a crate
3164 without conflict.
3165
3166 Therefore, if you plan to compile your crate as a library, you should annotate it with that information:
3167
3168 ~~~~
3169 // `lib.rs`
3170
3171 # #![crate_type = "lib"]
3172 #![crate_id = "farm#2.5"]
3173
3174 // ...
3175 # fn farm() {}
3176 ~~~~
3177
3178 You can also specify crate id information in a `extern crate` statement.  For
3179 example, these `extern crate` statements would both accept and select the
3180 crate define above:
3181
3182 ~~~~ {.ignore}
3183 extern crate farm;
3184 extern crate farm = "farm#2.5";
3185 extern crate my_farm = "farm";
3186 ~~~~
3187
3188 Other crate settings and metadata include things like enabling/disabling certain errors or warnings,
3189 or setting the crate type (library or executable) explicitly:
3190
3191 ~~~~
3192 // `lib.rs`
3193 // ...
3194
3195 // This crate is a library ("bin" is the default)
3196 #![crate_id = "farm#2.5"]
3197 #![crate_type = "lib"]
3198
3199 // Turn on a warning
3200 #[warn(non_camel_case_types)]
3201 # fn farm() {}
3202 ~~~~
3203
3204 ## A minimal example
3205
3206 Now for something that you can actually compile yourself.
3207
3208 We define two crates, and use one of them as a library in the other.
3209
3210 ~~~~
3211 // `world.rs`
3212 #![crate_id = "world#0.42"]
3213
3214 # mod secret_module_to_make_this_test_run {
3215 pub fn explore() -> &'static str { "world" }
3216 # }
3217 ~~~~
3218
3219 ~~~~ {.ignore}
3220 // `main.rs`
3221 extern crate world;
3222 fn main() { println!("hello {}", world::explore()); }
3223 ~~~~
3224
3225 Now compile and run like this (adjust to your platform if necessary):
3226
3227 ~~~~ {.notrust}
3228 > rustc --crate-type=lib world.rs  # compiles libworld-<HASH>-0.42.so
3229 > rustc main.rs -L .               # compiles main
3230 > ./main
3231 "hello world"
3232 ~~~~
3233
3234 Notice that the library produced contains the version in the file name
3235 as well as an inscrutable string of alphanumerics. As explained in the previous paragraph,
3236 these are both part of Rust's library versioning scheme. The alphanumerics are
3237 a hash representing the crate's id.
3238
3239 ## The standard library and the prelude
3240
3241 While reading the examples in this tutorial, you might have asked yourself where all
3242 those magical predefined items like `range` are coming from.
3243
3244 The truth is, there's nothing magical about them: They are all defined normally
3245 in the `std` library, which is a crate that ships with Rust.
3246
3247 The only magical thing that happens is that `rustc` automatically inserts this line into your crate root:
3248
3249 ~~~ {.ignore}
3250 extern crate std;
3251 ~~~
3252
3253 As well as this line into every module body:
3254
3255 ~~~ {.ignore}
3256 use std::prelude::*;
3257 ~~~
3258
3259 The role of the `prelude` module is to re-export common definitions from `std`.
3260
3261 This allows you to use common types and functions like `Option<T>` or `range`
3262 without needing to import them. And if you need something from `std` that's not in the prelude,
3263 you just have to import it with an `use` statement.
3264
3265 For example, it re-exports `range` which is defined in `std::iter::range`:
3266
3267 ~~~
3268 use iter_range = std::iter::range;
3269
3270 fn main() {
3271     // `range` is imported by default
3272     for _ in range(0, 10) {}
3273
3274     // Doesn't hinder you from importing it under a different name yourself
3275     for _ in iter_range(0, 10) {}
3276
3277     // Or from not using the automatic import.
3278     for _ in ::std::iter::range(0, 10) {}
3279 }
3280 ~~~
3281
3282 Both auto-insertions can be disabled with an attribute if necessary:
3283
3284 ~~~
3285 // In the crate root:
3286 #![no_std]
3287 ~~~
3288
3289 ~~~
3290 // In any module:
3291 #![no_implicit_prelude]
3292 ~~~
3293
3294 See the [API documentation][stddoc] for details.
3295
3296 [stddoc]: std/index.html
3297
3298 # What next?
3299
3300 Now that you know the essentials, check out any of the additional
3301 guides on individual topics.
3302
3303 * [Pointers][pointers]
3304 * [Lifetimes][lifetimes]
3305 * [Tasks and communication][tasks]
3306 * [Macros][macros]
3307 * [The foreign function interface][ffi]
3308 * [Containers and iterators][container]
3309 * [Documenting Rust code][rustdoc]
3310 * [Testing Rust code][testing]
3311 * [The Rust Runtime][runtime]
3312
3313 There is further documentation on the [wiki], however those tend to be even more out of date as this document.
3314
3315 [pointers]: guide-pointers.html
3316 [lifetimes]: guide-lifetimes.html
3317 [tasks]: guide-tasks.html
3318 [macros]: guide-macros.html
3319 [ffi]: guide-ffi.html
3320 [container]: guide-container.html
3321 [testing]: guide-testing.html
3322 [runtime]: guide-runtime.html
3323 [rustdoc]: rustdoc.html
3324 [wiki]: https://github.com/mozilla/rust/wiki/Docs
3325
3326 [wiki-packages]: https://github.com/mozilla/rust/wiki/Doc-packages,-editors,-and-other-tools