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