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