]> git.lizzy.rs Git - rust.git/blob - doc/tutorial.md
Fix bug in `match`ing struct patterns
[rust.git] / doc / tutorial.md
1 % The Rust Language Tutorial
2
3 # Introduction
4
5 Rust is a programming language with a focus on type safety, memory
6 safety, concurrency and performance. It is intended for writing
7 large-scale, high-performance software that is free from several
8 classes of common errors. Rust has a sophisticated memory model that
9 encourages efficient data structures and safe concurrency patterns,
10 forbidding invalid memory accesses that would otherwise cause
11 segmentation faults. It is statically typed and compiled ahead of
12 time.
13
14 As a multi-paradigm language, Rust supports writing code in
15 procedural, functional and object-oriented styles. Some of its
16 pleasant high-level features include:
17
18 * **Type inference.** Type annotations on local variable declarations
19   are optional.
20 * **Safe task-based concurrency.** Rust's lightweight tasks do not share
21   memory, instead communicating through messages.
22 * **Higher-order functions.** Efficient and flexible closures provide
23   iteration and other control structures
24 * **Pattern matching and algebraic data types.** Pattern matching on
25   Rust's enumeration types (a more powerful version of C's enums,
26   similar to algebraic data types in functional languages) is a
27   compact and expressive way to encode program logic.
28 * **Polymorphism.** Rust has type-parametric functions and
29   types, type classes and OO-style interfaces.
30
31 ## Scope
32
33 This is an introductory tutorial for the Rust programming language. It
34 covers the fundamentals of the language, including the syntax, the
35 type system and memory model, generics, and modules. [Additional
36 tutorials](#what-next) cover specific language features in greater
37 depth.
38
39 This tutorial assumes that the reader is already familiar with one or
40 more languages in the C family. Understanding of pointers and general
41 memory management techniques will help.
42
43 ## Conventions
44
45 Throughout the tutorial, language keywords and identifiers defined in
46 example code are displayed in `code font`.
47
48 Code snippets are indented, and also shown in a monospaced font. Not
49 all snippets constitute whole programs. For brevity, we'll often show
50 fragments of programs that don't compile on their own. To try them
51 out, you might have to wrap them in `fn main() { ... }`, and make sure
52 they don't contain references to names that aren't actually defined.
53
54 > ***Warning:*** Rust is a language under ongoing development. Notes
55 > about potential changes to the language, implementation
56 > deficiencies, and other caveats appear offset in blockquotes.
57
58 # Getting started
59
60 The Rust compiler currently must be built from a [tarball], unless you
61 are on Windows, in which case using the [installer][win-exe] is
62 recommended.
63
64 Since the Rust compiler is written in Rust, it must be built by
65 a precompiled "snapshot" version of itself (made in an earlier state
66 of development). As such, source builds require a connection to
67 the Internet, to fetch snapshots, and an OS that can execute the
68 available snapshot binaries.
69
70 Snapshot binaries are currently built and tested on several platforms:
71
72 * Windows (7, Server 2008 R2), x86 only
73 * Linux (various distributions), x86 and x86-64
74 * OSX 10.6 ("Snow Leopard") or greater, x86 and x86-64
75
76 You may find that other platforms work, but these are our "tier 1"
77 supported build environments that are most likely to work.
78
79 > ***Note:*** Windows users should read the detailed
80 > "[getting started][wiki-start]" notes on the wiki. Even when using
81 > the binary installer, the Windows build requires a MinGW installation,
82 > the precise details of which are not discussed here. Finally, `rustc` may
83 > need to be [referred to as `rustc.exe`][bug-3319]. It's a bummer, we
84 > know.
85
86 [bug-3319]: https://github.com/mozilla/rust/issues/3319
87 [wiki-start]: https://github.com/mozilla/rust/wiki/Note-getting-started-developing-Rust
88
89 To build from source you will also need the following prerequisite
90 packages:
91
92 * g++ 4.4 or clang++ 3.x
93 * python 2.6 or later (but not 3.x)
94 * perl 5.0 or later
95 * gnu make 3.81 or later
96 * curl
97
98 If you've fulfilled those prerequisites, something along these lines
99 should work.
100
101 ~~~~ {.notrust}
102 $ curl -O http://static.rust-lang.org/dist/rust-0.7.tar.gz
103 $ tar -xzf rust-0.7.tar.gz
104 $ cd rust-0.7
105 $ ./configure
106 $ make && make install
107 ~~~~
108
109 You may need to use `sudo make install` if you do not normally have
110 permission to modify the destination directory. The install locations
111 can be adjusted by passing a `--prefix` argument to
112 `configure`. Various other options are also supported: pass `--help`
113 for more information on them.
114
115 When complete, `make install` will place several programs into
116 `/usr/local/bin`: `rustc`, the Rust compiler; `rustdoc`, the
117 API-documentation tool; `rustpkg`, the Rust package manager;
118 `rusti`, the Rust REPL; and `rust`, a tool which acts both as a unified
119 interface for them, and for a few common command line scenarios.
120
121 [tarball]: http://static.rust-lang.org/dist/rust-0.7.tar.gz
122 [win-exe]: http://static.rust-lang.org/dist/rust-0.7-install.exe
123
124 ## Compiling your first program
125
126 Rust program files are, by convention, given the extension `.rs`. Say
127 we have a file `hello.rs` containing this program:
128
129 ~~~~
130 fn main() {
131     println("hello?");
132 }
133 ~~~~
134
135 If the Rust compiler was installed successfully, running `rustc
136 hello.rs` will produce an executable called `hello` (or `hello.exe` on
137 Windows) which, upon running, will likely do exactly what you expect.
138
139 The Rust compiler tries to provide useful information when it encounters an
140 error. If you introduce an error into the program (for example, by changing
141 `println` to some nonexistent function), and then compile it, you'll see
142 an error message like this:
143
144 ~~~~ {.notrust}
145 hello.rs:2:4: 2:16 error: unresolved name: print_with_unicorns
146 hello.rs:2     print_with_unicorns("hello?");
147                ^~~~~~~~~~~~~~~~~~~~~~~
148 ~~~~
149
150 In its simplest form, a Rust program is a `.rs` file with some types
151 and functions defined in it. If it has a `main` function, it can be
152 compiled to an executable. Rust does not allow code that's not a
153 declaration to appear at the top level of the file: all statements must
154 live inside a function.  Rust programs can also be compiled as
155 libraries, and included in other programs.
156
157 ## Using the rust tool
158
159 While using `rustc` directly to generate your executables, and then
160 running them manually is a perfectly valid way to test your code,
161 for smaller projects, prototypes, or if you're a beginner, it might be
162 more convenient to use the `rust` tool.
163
164 The `rust` tool provides central access to the other rust tools,
165 as well as handy shortcuts for directly running source files.
166 For example, if you have a file `foo.rs` in your current directory,
167 `rust run foo.rs` would attempt to compile it and, if successful,
168 directly run the resulting binary.
169
170 To get a list of all available commands, simply call `rust` without any
171 argument.
172
173 ## Editing Rust code
174
175 There are vim highlighting and indentation scripts in the Rust source
176 distribution under `src/etc/vim/`. There is an emacs mode under
177 `src/etc/emacs/` called `rust-mode`, but do read the instructions
178 included in that directory. In particular, if you are running emacs
179 24, then using emacs's internal package manager to install `rust-mode`
180 is the easiest way to keep it up to date. There is also a package for
181 Sublime Text 2, available both [standalone][sublime] and through
182 [Sublime Package Control][sublime-pkg], and support for Kate
183 under `src/etc/kate`.
184
185 There is ctags support via `src/etc/ctags.rust`, but many other
186 tools and editors are not yet supported. If you end up writing a Rust
187 mode for your favorite editor, let us know so that we can link to it.
188
189 [sublime]: http://github.com/dbp/sublime-rust
190 [sublime-pkg]: http://wbond.net/sublime_packages/package_control
191
192 # Syntax basics
193
194 Assuming you've programmed in any C-family language (C++, Java,
195 JavaScript, C#, or PHP), Rust will feel familiar. Code is arranged
196 in blocks delineated by curly braces; there are control structures
197 for branching and looping, like the familiar `if` and `while`; function
198 calls are written `myfunc(arg1, arg2)`; operators are written the same
199 and mostly have the same precedence as in C; comments are again like C;
200 module names are separated with double-colon (`::`) as with C++.
201
202 The main surface difference to be aware of is that the condition at
203 the head of control structures like `if` and `while` does not require
204 parentheses, while their bodies *must* be wrapped in
205 braces. Single-statement, unbraced bodies are not allowed.
206
207 ~~~~
208 # mod universe { pub fn recalibrate() -> bool { true } }
209 fn main() {
210     /* A simple loop */
211     loop {
212         // A tricky calculation
213         if universe::recalibrate() {
214             return;
215         }
216     }
217 }
218 ~~~~
219
220 The `let` keyword introduces a local variable. Variables are immutable by
221 default. To introduce a local variable that you can re-assign later, use `let
222 mut` instead.
223
224 ~~~~
225 let hi = "hi";
226 let mut count = 0;
227
228 while count < 10 {
229     println(fmt!("count: %?", count));
230     count += 1;
231 }
232 ~~~~
233
234 Although Rust can almost always infer the types of local variables, you
235 can specify a variable's type by following it with a colon, then the type
236 name. Static items, on the other hand, always require a type annotation.
237
238 ~~~~
239 static MONSTER_FACTOR: float = 57.8;
240 let monster_size = MONSTER_FACTOR * 10.0;
241 let monster_size: int = 50;
242 ~~~~
243
244 Local variables may shadow earlier declarations, as in the previous example:
245 `monster_size` was first declared as a `float`, and then a second
246 `monster_size` was declared as an `int`. If you were to actually compile this
247 example, though, the compiler would determine that the first `monster_size` is
248 unused and issue a warning (because this situation is likely to indicate a
249 programmer error). For occasions where unused variables are intentional, their
250 names may be prefixed with an underscore to silence the warning, like `let
251 _monster_size = 50;`.
252
253 Rust identifiers start with an alphabetic
254 character or an underscore, and after that may contain any sequence of
255 alphabetic characters, numbers, or underscores. The preferred style is to
256 write function, variable, and module names with lowercase letters, using
257 underscores where they help readability, while writing types in camel case.
258
259 ~~~
260 let my_variable = 100;
261 type MyType = int;     // primitive types are _not_ camel case
262 ~~~
263
264 ## Expressions and semicolons
265
266 Though it isn't apparent in all code, there is a fundamental
267 difference between Rust's syntax and predecessors like C.
268 Many constructs that are statements in C are expressions
269 in Rust, allowing code to be more concise. For example, you might
270 write a piece of code like this:
271
272 ~~~~
273 # let item = "salad";
274 let price;
275 if item == "salad" {
276     price = 3.50;
277 } else if item == "muffin" {
278     price = 2.25;
279 } else {
280     price = 2.00;
281 }
282 ~~~~
283
284 But, in Rust, you don't have to repeat the name `price`:
285
286 ~~~~
287 # let item = "salad";
288 let price =
289     if item == "salad" {
290         3.50
291     } else if item == "muffin" {
292         2.25
293     } else {
294         2.00
295     };
296 ~~~~
297
298 Both pieces of code are exactly equivalent: they assign a value to
299 `price` depending on the condition that holds. Note that there
300 are no semicolons in the blocks of the second snippet. This is
301 important: the lack of a semicolon after the last statement in a
302 braced block gives the whole block the value of that last expression.
303
304 Put another way, the semicolon in Rust *ignores the value of an expression*.
305 Thus, if the branches of the `if` had looked like `{ 4; }`, the above example
306 would simply assign `()` (nil or void) to `price`. But without the semicolon, each
307 branch has a different value, and `price` gets the value of the branch that
308 was taken.
309
310 In short, everything that's not a declaration (declarations are `let` for
311 variables; `fn` for functions; and any top-level named items such as
312 [traits](#traits), [enum types](#enums), and static items) is an
313 expression, including function bodies.
314
315 ~~~~
316 fn is_four(x: int) -> bool {
317    // No need for a return statement. The result of the expression
318    // is used as the return value.
319    x == 4
320 }
321 ~~~~
322
323 ## Primitive types and literals
324
325 There are general signed and unsigned integer types, `int` and `uint`,
326 as well as 8-, 16-, 32-, and 64-bit variants, `i8`, `u16`, etc.
327 Integers can be written in decimal (`144`), hexadecimal (`0x90`), or
328 binary (`0b10010000`) base. Each integral type has a corresponding literal
329 suffix that can be used to indicate the type of a literal: `i` for `int`,
330 `u` for `uint`, `i8` for the `i8` type.
331
332 In the absence of an integer literal suffix, Rust will infer the
333 integer type based on type annotations and function signatures in the
334 surrounding program. In the absence of any type information at all,
335 Rust will assume that an unsuffixed integer literal has type
336 `int`.
337
338 ~~~~
339 let a = 1;       // a is an int
340 let b = 10i;     // b is an int, due to the 'i' suffix
341 let c = 100u;    // c is a uint
342 let d = 1000i32; // d is an i32
343 ~~~~
344
345 There are three floating-point types: `float`, `f32`, and `f64`.
346 Floating-point numbers are written `0.0`, `1e6`, or `2.1e-4`.
347 Like integers, floating-point literals are inferred to the correct type.
348 Suffixes `f`, `f32`, and `f64` can be used to create literals of a specific type.
349
350 The keywords `true` and `false` produce literals of type `bool`.
351
352 Characters, the `char` type, are four-byte Unicode codepoints,
353 whose literals are written between single quotes, as in `'x'`.
354 Just like C, Rust understands a number of character escapes, using the backslash
355 character, such as `\n`, `\r`, and `\t`. String literals,
356 written between double quotes, allow the same escape sequences.
357 More on strings [later](#vectors-and-strings).
358
359 The nil type, written `()`, has a single value, also written `()`.
360
361 ## Operators
362
363 Rust's set of operators contains very few surprises. Arithmetic is done with
364 `*`, `/`, `%`, `+`, and `-` (multiply, quotient, remainder, add, and subtract). `-` is
365 also a unary prefix operator that negates numbers. As in C, the bitwise operators
366 `>>`, `<<`, `&`, `|`, and `^` are also supported.
367
368 Note that, if applied to an integer value, `!` flips all the bits (like `~` in
369 C).
370
371 The comparison operators are the traditional `==`, `!=`, `<`, `>`,
372 `<=`, and `>=`. Short-circuiting (lazy) boolean operators are written
373 `&&` (and) and `||` (or).
374
375 For type casting, Rust uses the binary `as` operator.  It takes an
376 expression on the left side and a type on the right side and will,
377 if a meaningful conversion exists, convert the result of the
378 expression to the given type.
379
380 ~~~~
381 let x: float = 4.0;
382 let y: uint = x as uint;
383 assert!(y == 4u);
384 ~~~~
385
386 ## Syntax extensions
387
388 *Syntax extensions* are special forms that are not built into the language,
389 but are instead provided by the libraries. To make it clear to the reader when
390 a name refers to a syntax extension, the names of all syntax extensions end
391 with `!`. The standard library defines a few syntax extensions, the most
392 useful of which is `fmt!`, a `sprintf`-style text formatter that you will
393 often see in examples.
394
395 `fmt!` supports most of the directives that [printf][pf] supports, but unlike
396 printf, will give you a compile-time error when the types of the directives
397 don't match the types of the arguments.
398
399 ~~~~
400 # let mystery_object = ();
401
402 println(fmt!("%s is %d", "the answer", 43));
403
404 // %? will conveniently print any type
405 println(fmt!("what is this thing: %?", mystery_object));
406 ~~~~
407
408 [pf]: http://en.cppreference.com/w/cpp/io/c/fprintf
409
410 You can define your own syntax extensions with the macro system. For details, see the [macro tutorial][macros].
411
412 # Control structures
413
414 ## Conditionals
415
416 We've seen `if` expressions a few times already. To recap, braces are
417 compulsory, an `if` can have an optional `else` clause, and multiple
418 `if`/`else` constructs can be chained together:
419
420 ~~~~
421 if false {
422     println("that's odd");
423 } else if true {
424     println("right");
425 } else {
426     println("neither true nor false");
427 }
428 ~~~~
429
430 The condition given to an `if` construct *must* be of type `bool` (no
431 implicit conversion happens). If the arms are blocks that have a
432 value, this value must be of the same type for every arm in which
433 control reaches the end of the block:
434
435 ~~~~
436 fn signum(x: int) -> int {
437     if x < 0 { -1 }
438     else if x > 0 { 1 }
439     else { return 0 }
440 }
441 ~~~~
442
443 ## Pattern matching
444
445 Rust's `match` construct is a generalized, cleaned-up version of C's
446 `switch` construct. You provide it with a value and a number of
447 *arms*, each labelled with a pattern, and the code compares the value
448 against each pattern in order until one matches. The matching pattern
449 executes its corresponding arm.
450
451 ~~~~
452 # let my_number = 1;
453 match my_number {
454   0     => println("zero"),
455   1 | 2 => println("one or two"),
456   3..10 => println("three to ten"),
457   _     => println("something else")
458 }
459 ~~~~
460
461 Unlike in C, there is no "falling through" between arms: only one arm
462 executes, and it doesn't have to explicitly `break` out of the
463 construct when it is finished.
464
465 A `match` arm consists of a *pattern*, then an arrow `=>`, followed by
466 an *action* (expression). Literals are valid patterns and match only
467 their own value. A single arm may match multiple different patterns by
468 combining them with the pipe operator (`|`), so long as every pattern
469 binds the same set of variables. Ranges of numeric literal patterns
470 can be expressed with two dots, as in `M..N`. The underscore (`_`) is
471 a wildcard pattern that matches any single value. The asterisk (`*`)
472 is a different wildcard that can match one or more fields in an `enum`
473 variant.
474
475 The patterns in a match arm are followed by a fat arrow, `=>`, then an
476 expression to evaluate. Each case is separated by commas. It's often
477 convenient to use a block expression for each case, in which case the
478 commas are optional.
479
480 ~~~
481 # let my_number = 1;
482 match my_number {
483   0 => { println("zero") }
484   _ => { println("something else") }
485 }
486 ~~~
487
488 `match` constructs must be *exhaustive*: they must have an arm
489 covering every possible case. For example, the typechecker would
490 reject the previous example if the arm with the wildcard pattern was
491 omitted.
492
493 A powerful application of pattern matching is *destructuring*:
494 matching in order to bind names to the contents of data
495 types.
496
497 > ***Note:*** The following code makes use of tuples (`(float, float)`) which
498 > are explained in section 5.3. For now you can think of tuples as a list of
499 > items.
500
501 ~~~~
502 use std::float;
503 use std::num::atan;
504 fn angle(vector: (float, float)) -> float {
505     let pi = float::consts::pi;
506     match vector {
507       (0f, y) if y < 0f => 1.5 * pi,
508       (0f, y) => 0.5 * pi,
509       (x, y) => atan(y / x)
510     }
511 }
512 ~~~~
513
514 A variable name in a pattern matches any value, *and* binds that name
515 to the value of the matched value inside of the arm's action. Thus, `(0f,
516 y)` matches any tuple whose first element is zero, and binds `y` to
517 the second element. `(x, y)` matches any two-element tuple, and binds both
518 elements to variables.
519
520 Any `match` arm can have a guard clause (written `if EXPR`), called a
521 *pattern guard*, which is an expression of type `bool` that
522 determines, after the pattern is found to match, whether the arm is
523 taken or not. The variables bound by the pattern are in scope in this
524 guard expression. The first arm in the `angle` example shows an
525 example of a pattern guard.
526
527 You've already seen simple `let` bindings, but `let` is a little
528 fancier than you've been led to believe. It, too, supports destructuring
529 patterns. For example, you can write this to extract the fields from a
530 tuple, introducing two variables at once: `a` and `b`.
531
532 ~~~~
533 # fn get_tuple_of_two_ints() -> (int, int) { (1, 1) }
534 let (a, b) = get_tuple_of_two_ints();
535 ~~~~
536
537 Let bindings only work with _irrefutable_ patterns: that is, patterns
538 that can never fail to match. This excludes `let` from matching
539 literals and most `enum` variants.
540
541 ## Loops
542
543 `while` denotes a loop that iterates as long as its given condition
544 (which must have type `bool`) evaluates to `true`. Inside a loop, the
545 keyword `break` aborts the loop, and `loop` aborts the current
546 iteration and continues with the next.
547
548 ~~~~
549 let mut cake_amount = 8;
550 while cake_amount > 0 {
551     cake_amount -= 1;
552 }
553 ~~~~
554
555 `loop` denotes an infinite loop, and is the preferred way of writing `while true`:
556
557 ~~~~
558 use std::int;
559 let mut x = 5;
560 loop {
561     x += x - 3;
562     if x % 5 == 0 { break; }
563     println(int::to_str(x));
564 }
565 ~~~~
566
567 This code prints out a weird sequence of numbers and stops as soon as
568 it finds one that can be divided by five.
569
570 # Data structures
571
572 ## Structs
573
574 Rust struct types must be declared before they are used using the `struct`
575 syntax: `struct Name { field1: T1, field2: T2 [, ...] }`, where `T1`, `T2`,
576 ... denote types. To construct a struct, use the same syntax, but leave off
577 the `struct`: for example: `Point { x: 1.0, y: 2.0 }`.
578
579 Structs are quite similar to C structs and are even laid out the same way in
580 memory (so you can read from a Rust struct in C, and vice-versa). Use the dot
581 operator to access struct fields, as in `mypoint.x`.
582
583 ~~~~
584 struct Point {
585     x: float,
586     y: float
587 }
588 ~~~~
589
590 Inherited mutability means that any field of a struct may be mutable, if the
591 struct is in a mutable slot (or a field of a struct in a mutable slot, and
592 so forth).
593
594 With a value (say, `mypoint`) of such a type in a mutable location, you can do
595 `mypoint.y += 1.0`. But in an immutable location, such an assignment to a
596 struct without inherited mutability would result in a type error.
597
598 ~~~~ {.xfail-test}
599 # struct Point { x: float, y: float }
600 let mut mypoint = Point { x: 1.0, y: 1.0 };
601 let origin = Point { x: 0.0, y: 0.0 };
602
603 mypoint.y += 1.0; // mypoint is mutable, and its fields as well
604 origin.y += 1.0; // ERROR: assigning to immutable field
605 ~~~~
606
607 `match` patterns destructure structs. The basic syntax is
608 `Name { fieldname: pattern, ... }`:
609
610 ~~~~
611 # struct Point { x: float, y: float }
612 # let mypoint = Point { x: 0.0, y: 0.0 };
613 match mypoint {
614     Point { x: 0.0, y: yy } => { println(yy.to_str());                     }
615     Point { x: xx,  y: yy } => { println(xx.to_str() + " " + yy.to_str()); }
616 }
617 ~~~~
618
619 In general, the field names of a struct do not have to appear in the same
620 order they appear in the type. When you are not interested in all
621 the fields of a struct, a struct pattern may end with `, _` (as in
622 `Name { field1, _ }`) to indicate that you're ignoring all other fields.
623 Additionally, struct fields have a shorthand matching form that simply
624 reuses the field name as the binding name.
625
626 ~~~
627 # struct Point { x: float, y: float }
628 # let mypoint = Point { x: 0.0, y: 0.0 };
629 match mypoint {
630     Point { x, _ } => { println(x.to_str()) }
631 }
632 ~~~
633
634 ## Enums
635
636 Enums are datatypes that have several alternate representations. For
637 example, consider the type shown earlier:
638
639 ~~~~
640 # struct Point { x: float, y: float }
641 enum Shape {
642     Circle(Point, float),
643     Rectangle(Point, Point)
644 }
645 ~~~~
646
647 A value of this type is either a `Circle`, in which case it contains a
648 `Point` struct and a float, or a `Rectangle`, in which case it contains
649 two `Point` structs. The run-time representation of such a value
650 includes an identifier of the actual form that it holds, much like the
651 "tagged union" pattern in C, but with better static guarantees.
652
653 The above declaration will define a type `Shape` that can refer to
654 such shapes, and two functions, `Circle` and `Rectangle`, which can be
655 used to construct values of the type (taking arguments of the
656 specified types). So `Circle(Point { x: 0f, y: 0f }, 10f)` is the way to
657 create a new circle.
658
659 Enum variants need not have parameters. This `enum` declaration,
660 for example, is equivalent to a C enum:
661
662 ~~~~
663 enum Direction {
664     North,
665     East,
666     South,
667     West
668 }
669 ~~~~
670
671 This declaration defines `North`, `East`, `South`, and `West` as constants,
672 all of which have type `Direction`.
673
674 When an enum is C-like (that is, when none of the variants have
675 parameters), it is possible to explicitly set the discriminator values
676 to a constant value:
677
678 ~~~~
679 enum Color {
680   Red = 0xff0000,
681   Green = 0x00ff00,
682   Blue = 0x0000ff
683 }
684 ~~~~
685
686 If an explicit discriminator is not specified for a variant, the value
687 defaults to the value of the previous variant plus one. If the first
688 variant does not have a discriminator, it defaults to 0. For example,
689 the value of `North` is 0, `East` is 1, `South` is 2, and `West` is 3.
690
691 When an enum is C-like, you can apply the `as` cast operator to
692 convert it to its discriminator value as an `int`.
693
694 For enum types with multiple variants, destructuring is the only way to
695 get at their contents. All variant constructors can be used as
696 patterns, as in this definition of `area`:
697
698 ~~~~
699 use std::float;
700 # struct Point {x: float, y: float}
701 # enum Shape { Circle(Point, float), Rectangle(Point, Point) }
702 fn area(sh: Shape) -> float {
703     match sh {
704         Circle(_, size) => float::consts::pi * size * size,
705         Rectangle(Point { x, y }, Point { x: x2, y: y2 }) => (x2 - x) * (y2 - y)
706     }
707 }
708 ~~~~
709
710 You can write a lone `_` to ignore an individual field, and can
711 ignore all fields of a variant like: `Circle(*)`. As in their
712 introduction form, nullary enum patterns are written without
713 parentheses.
714
715 ~~~~
716 # struct Point { x: float, y: float }
717 # enum Direction { North, East, South, West }
718 fn point_from_direction(dir: Direction) -> Point {
719     match dir {
720         North => Point { x:  0f, y:  1f },
721         East  => Point { x:  1f, y:  0f },
722         South => Point { x:  0f, y: -1f },
723         West  => Point { x: -1f, y:  0f }
724     }
725 }
726 ~~~~
727
728 Enum variants may also be structs. For example:
729
730 ~~~~
731 use std::float;
732 # struct Point { x: float, y: float }
733 # fn square(x: float) -> float { x * x }
734 enum Shape {
735     Circle { center: Point, radius: float },
736     Rectangle { top_left: Point, bottom_right: Point }
737 }
738 fn area(sh: Shape) -> float {
739     match sh {
740         Circle { radius: radius, _ } => float::consts::pi * square(radius),
741         Rectangle { top_left: top_left, bottom_right: bottom_right } => {
742             (bottom_right.x - top_left.x) * (bottom_right.y - top_left.y)
743         }
744     }
745 }
746 ~~~~
747
748 ## Tuples
749
750 Tuples in Rust behave exactly like structs, except that their fields
751 do not have names. Thus, you cannot access their fields with dot notation.
752 Tuples can have any arity except for 0 (though you may consider
753 unit, `()`, as the empty tuple if you like).
754
755 ~~~~
756 let mytup: (int, int, float) = (10, 20, 30.0);
757 match mytup {
758   (a, b, c) => info!(a + b + (c as int))
759 }
760 ~~~~
761
762 ## Tuple structs
763
764 Rust also has _tuple structs_, which behave like both structs and tuples,
765 except that, unlike tuples, tuple structs have names (so `Foo(1, 2)` has a
766 different type from `Bar(1, 2)`), and tuple structs' _fields_ do not have
767 names.
768
769 For example:
770 ~~~~
771 struct MyTup(int, int, float);
772 let mytup: MyTup = MyTup(10, 20, 30.0);
773 match mytup {
774   MyTup(a, b, c) => info!(a + b + (c as int))
775 }
776 ~~~~
777
778 <a name="newtype"></a>
779
780 There is a special case for tuple structs with a single field, which are
781 sometimes called "newtypes" (after Haskell's "newtype" feature). These are
782 used to define new types in such a way that the new name is not just a
783 synonym for an existing type but is rather its own distinct type.
784
785 ~~~~
786 struct GizmoId(int);
787 ~~~~
788
789 For convenience, you can extract the contents of such a struct with the
790 dereference (`*`) unary operator:
791
792 ~~~~
793 # struct GizmoId(int);
794 let my_gizmo_id: GizmoId = GizmoId(10);
795 let id_int: int = *my_gizmo_id;
796 ~~~~
797
798 Types like this can be useful to differentiate between data that have
799 the same type but must be used in different ways.
800
801 ~~~~
802 struct Inches(int);
803 struct Centimeters(int);
804 ~~~~
805
806 The above definitions allow for a simple way for programs to avoid
807 confusing numbers that correspond to different units.
808
809 # Functions
810
811 We've already seen several function definitions. Like all other static
812 declarations, such as `type`, functions can be declared both at the
813 top level and inside other functions (or in modules, which we'll come
814 back to [later](#modules-and-crates)). The `fn` keyword introduces a
815 function. A function has an argument list, which is a parenthesized
816 list of `expr: type` pairs separated by commas. An arrow `->`
817 separates the argument list and the function's return type.
818
819 ~~~~
820 fn line(a: int, b: int, x: int) -> int {
821     return a * x + b;
822 }
823 ~~~~
824
825 The `return` keyword immediately returns from the body of a function. It
826 is optionally followed by an expression to return. A function can
827 also return a value by having its top-level block produce an
828 expression.
829
830 ~~~~
831 fn line(a: int, b: int, x: int) -> int {
832     a * x + b
833 }
834 ~~~~
835
836 It's better Rust style to write a return value this way instead of
837 writing an explicit `return`. The utility of `return` comes in when
838 returning early from a function. Functions that do not return a value
839 are said to return nil, `()`, and both the return type and the return
840 value may be omitted from the definition. The following two functions
841 are equivalent.
842
843 ~~~~
844 fn do_nothing_the_hard_way() -> () { return (); }
845
846 fn do_nothing_the_easy_way() { }
847 ~~~~
848
849 Ending the function with a semicolon like so is equivalent to returning `()`.
850
851 ~~~~
852 fn line(a: int, b: int, x: int) -> int { a * x + b  }
853 fn oops(a: int, b: int, x: int) -> ()  { a * x + b; }
854
855 assert!(8 == line(5, 3, 1));
856 assert!(() == oops(5, 3, 1));
857 ~~~~
858
859 As with `match` expressions and `let` bindings, function arguments support
860 pattern destructuring. Like `let`, argument patterns must be irrefutable,
861 as in this example that unpacks the first value from a tuple and returns it.
862
863 ~~~
864 fn first((value, _): (int, float)) -> int { value }
865 ~~~
866
867 # Destructors
868
869 A *destructor* is a function responsible for cleaning up the resources used by
870 an object when it is no longer accessible. Destructors can be defined to handle
871 the release of resources like files, sockets and heap memory.
872
873 Objects are never accessible after their destructor has been called, so there
874 are no dynamic failures from accessing freed resources. When a task fails, the
875 destructors of all objects in the task are called.
876
877 The `~` sigil represents a unique handle for a memory allocation on the heap:
878
879 ~~~~
880 {
881     // an integer allocated on the heap
882     let y = ~10;
883 }
884 // the destructor frees the heap memory as soon as `y` goes out of scope
885 ~~~~
886
887 Rust includes syntax for heap memory allocation in the language since it's
888 commonly used, but the same semantics can be implemented by a type with a
889 custom destructor.
890
891 # Ownership
892
893 Rust formalizes the concept of object ownership to delegate management of an
894 object's lifetime to either a variable or a task-local garbage collector. An
895 object's owner is responsible for managing the lifetime of the object by
896 calling the destructor, and the owner determines whether the object is mutable.
897
898 Ownership is recursive, so mutability is inherited recursively and a destructor
899 destroys the contained tree of owned objects. Variables are top-level owners
900 and destroy the contained object when they go out of scope. A box managed by
901 the garbage collector starts a new ownership tree, and the destructor is called
902 when it is collected.
903
904 ~~~~
905 // the struct owns the objects contained in the `x` and `y` fields
906 struct Foo { x: int, y: ~int }
907
908 {
909     // `a` is the owner of the struct, and thus the owner of the struct's fields
910     let a = Foo { x: 5, y: ~10 };
911 }
912 // when `a` goes out of scope, the destructor for the `~int` in the struct's
913 // field is called
914
915 // `b` is mutable, and the mutability is inherited by the objects it owns
916 let mut b = Foo { x: 5, y: ~10 };
917 b.x = 10;
918 ~~~~
919
920 If an object doesn't contain garbage-collected boxes, it consists of a single
921 ownership tree and is given the `Owned` trait which allows it to be sent
922 between tasks. Custom destructors can only be implemented directly on types
923 that are `Owned`, but garbage-collected boxes can still *contain* types with
924 custom destructors.
925
926 # Boxes
927
928 Many modern languages represent values as pointers to heap memory by
929 default. In contrast, Rust, like C and C++, represents such types directly.
930 Another way to say this is that aggregate data in Rust are *unboxed*. This
931 means that if you `let x = Point { x: 1f, y: 1f };`, you are creating a struct
932 on the stack. If you then copy it into a data structure, you copy the entire
933 struct, not just a pointer.
934
935 For small structs like `Point`, this is usually more efficient than allocating
936 memory and indirecting through a pointer. But for big structs, or mutable
937 state, it can be useful to have a single copy on the stack or on the heap, and
938 refer to that through a pointer.
939
940 ## Owned boxes
941
942 An owned box (`~`) is a uniquely owned allocation on the heap. It inherits the
943 mutability and lifetime of the owner as it would if there was no box:
944
945 ~~~~
946 let x = 5; // immutable
947 let mut y = 5; // mutable
948 y += 2;
949
950 let x = ~5; // immutable
951 let mut y = ~5; // mutable
952 *y += 2; // the * operator is needed to access the contained value
953 ~~~~
954
955 The purpose of an owned box is to add a layer of indirection in order to create
956 recursive data structures or cheaply pass around an object larger than a
957 pointer. Since an owned box has a unique owner, it can only be used to
958 represent a tree data structure.
959
960 The following struct won't compile, because the lack of indirection would mean
961 it has an infinite size:
962
963 ~~~~ {.xfail-test}
964 struct Foo {
965     child: Option<Foo>
966 }
967 ~~~~
968
969 > ***Note:*** The `Option` type is an enum that represents an *optional* value.
970 > It's comparable to a nullable pointer in many other languages, but stores the
971 > contained value unboxed.
972
973 Adding indirection with an owned pointer allocates the child outside of the
974 struct on the heap, which makes it a finite size and won't result in a
975 compile-time error:
976
977 ~~~~
978 struct Foo {
979     child: Option<~Foo>
980 }
981 ~~~~
982
983 ## Managed boxes
984
985 A managed box (`@`) is a heap allocation with the lifetime managed by a
986 task-local garbage collector. It will be destroyed at some point after there
987 are no references left to the box, no later than the end of the task. Managed
988 boxes lack an owner, so they start a new ownership tree and don't inherit
989 mutability. They do own the contained object, and mutability is defined by the
990 type of the managed box (`@` or `@mut`). An object containing a managed box is
991 not `Owned`, and can't be sent between tasks.
992
993 ~~~~
994 let a = @5; // immutable
995
996 let mut b = @5; // mutable variable, immutable box
997 b = @10;
998
999 let c = @mut 5; // immutable variable, mutable box
1000 *c = 10;
1001
1002 let mut d = @mut 5; // mutable variable, mutable box
1003 *d += 5;
1004 d = @mut 15;
1005 ~~~~
1006
1007 A mutable variable and an immutable variable can refer to the same box, given
1008 that their types are compatible. Mutability of a box is a property of its type,
1009 however, so for example a mutable handle to an immutable box cannot be
1010 assigned a reference to a mutable box.
1011
1012 ~~~~
1013 let a = @1;     // immutable box
1014 let b = @mut 2; // mutable box
1015
1016 let mut c : @int;       // declare a variable with type managed immutable int
1017 let mut d : @mut int;   // and one of type managed mutable int
1018
1019 c = a;          // box type is the same, okay
1020 d = b;          // box type is the same, okay
1021 ~~~~
1022
1023 ~~~~ {.xfail-test}
1024 // but b cannot be assigned to c, or a to d
1025 c = b;          // error
1026 ~~~~
1027
1028 # Move semantics
1029
1030 Rust uses a shallow copy for parameter passing, assignment and returning values
1031 from functions. A shallow copy is considered a move of ownership if the
1032 ownership tree of the copied value includes an owned box or a type with a
1033 custom destructor. After a value has been moved, it can no longer be used from
1034 the source location and will not be destroyed there.
1035
1036 ~~~~
1037 let x = ~5;
1038 let y = x.clone(); // y is a newly allocated box
1039 let z = x; // no new memory allocated, x can no longer be used
1040 ~~~~
1041
1042 Since in owned boxes mutability is a property of the owner, not the
1043 box, mutable boxes may become immutable when they are moved, and vice-versa.
1044
1045 ~~~~
1046 let r = ~13;
1047 let mut s = r; // box becomes mutable
1048 *s += 1;
1049 let t = s; // box becomes immutable
1050 ~~~~
1051
1052 # Borrowed pointers
1053
1054 Rust's borrowed pointers are a general purpose reference type. In contrast with
1055 owned boxes, where the holder of an owned box is the owner of the pointed-to
1056 memory, borrowed pointers never imply ownership. A pointer can be borrowed to
1057 any object, and the compiler verifies that it cannot outlive the lifetime of
1058 the object.
1059
1060 As an example, consider a simple struct type, `Point`:
1061
1062 ~~~
1063 struct Point {
1064     x: float,
1065     y: float
1066 }
1067 ~~~~
1068
1069 We can use this simple definition to allocate points in many different
1070 ways. For example, in this code, each of these three local variables
1071 contains a point, but allocated in a different location:
1072
1073 ~~~
1074 # struct Point { x: float, y: float }
1075 let on_the_stack : Point  =  Point { x: 3.0, y: 4.0 };
1076 let managed_box  : @Point = @Point { x: 5.0, y: 1.0 };
1077 let owned_box    : ~Point = ~Point { x: 7.0, y: 9.0 };
1078 ~~~
1079
1080 Suppose we want to write a procedure that computes the distance
1081 between any two points, no matter where they are stored. For example,
1082 we might like to compute the distance between `on_the_stack` and
1083 `managed_box`, or between `managed_box` and `owned_box`. One option is
1084 to define a function that takes two arguments of type point—that is,
1085 it takes the points by value. But this will cause the points to be
1086 copied when we call the function. For points, this is probably not so
1087 bad, but often copies are expensive. So we’d like to define a function
1088 that takes the points by pointer. We can use borrowed pointers to do this:
1089
1090 ~~~
1091 # struct Point { x: float, y: float }
1092 # fn sqrt(f: float) -> float { 0f }
1093 fn compute_distance(p1: &Point, p2: &Point) -> float {
1094     let x_d = p1.x - p2.x;
1095     let y_d = p1.y - p2.y;
1096     sqrt(x_d * x_d + y_d * y_d)
1097 }
1098 ~~~
1099
1100 Now we can call `compute_distance()` in various ways:
1101
1102 ~~~
1103 # struct Point{ x: float, y: float };
1104 # let on_the_stack : Point  =  Point { x: 3.0, y: 4.0 };
1105 # let managed_box  : @Point = @Point { x: 5.0, y: 1.0 };
1106 # let owned_box    : ~Point = ~Point { x: 7.0, y: 9.0 };
1107 # fn compute_distance(p1: &Point, p2: &Point) -> float { 0f }
1108 compute_distance(&on_the_stack, managed_box);
1109 compute_distance(managed_box, owned_box);
1110 ~~~
1111
1112 Here the `&` operator is used to take the address of the variable
1113 `on_the_stack`; this is because `on_the_stack` has the type `Point`
1114 (that is, a struct value) and we have to take its address to get a
1115 value. We also call this _borrowing_ the local variable
1116 `on_the_stack`, because we are creating an alias: that is, another
1117 route to the same data.
1118
1119 In the case of the boxes `managed_box` and `owned_box`, however, no
1120 explicit action is necessary. The compiler will automatically convert
1121 a box like `@point` or `~point` to a borrowed pointer like
1122 `&point`. This is another form of borrowing; in this case, the
1123 contents of the managed/owned box are being lent out.
1124
1125 Whenever a value is borrowed, there are some limitations on what you
1126 can do with the original. For example, if the contents of a variable
1127 have been lent out, you cannot send that variable to another task, nor
1128 will you be permitted to take actions that might cause the borrowed
1129 value to be freed or to change its type. This rule should make
1130 intuitive sense: you must wait for a borrowed value to be returned
1131 (that is, for the borrowed pointer to go out of scope) before you can
1132 make full use of it again.
1133
1134 For a more in-depth explanation of borrowed pointers, read the
1135 [borrowed pointer tutorial][borrowtut].
1136
1137 [borrowtut]: tutorial-borrowed-ptr.html
1138
1139 ## Freezing
1140
1141 Borrowing an immutable pointer to an object freezes it and prevents mutation.
1142 `Owned` objects have freezing enforced statically at compile-time.
1143
1144 ~~~~
1145 let mut x = 5;
1146 {
1147     let y = &x; // x is now frozen, it cannot be modified
1148 }
1149 // x is now unfrozen again
1150 # x = 3;
1151 ~~~~
1152
1153 Mutable managed boxes handle freezing dynamically when any of their contents
1154 are borrowed, and the task will fail if an attempt to modify them is made while
1155 they are frozen:
1156
1157 ~~~~
1158 let x = @mut 5;
1159 let y = x;
1160 {
1161     let z = &*y; // the managed box is now frozen
1162     // modifying it through x or y will cause a task failure
1163 }
1164 // the box is now unfrozen again
1165 ~~~~
1166
1167 # Dereferencing pointers
1168
1169 Rust uses the unary star operator (`*`) to access the contents of a
1170 box or pointer, similarly to C.
1171
1172 ~~~
1173 let managed = @10;
1174 let owned = ~20;
1175 let borrowed = &30;
1176
1177 let sum = *managed + *owned + *borrowed;
1178 ~~~
1179
1180 Dereferenced mutable pointers may appear on the left hand side of
1181 assignments. Such an assignment modifies the value that the pointer
1182 points to.
1183
1184 ~~~
1185 let managed = @mut 10;
1186 let mut owned = ~20;
1187
1188 let mut value = 30;
1189 let borrowed = &mut value;
1190
1191 *managed = *owned + 10;
1192 *owned = *borrowed + 100;
1193 *borrowed = *managed + 1000;
1194 ~~~
1195
1196 Pointers have high operator precedence, but lower precedence than the
1197 dot operator used for field and method access. This precedence order
1198 can sometimes make code awkward and parenthesis-filled.
1199
1200 ~~~
1201 # struct Point { x: float, y: float }
1202 # enum Shape { Rectangle(Point, Point) }
1203 # impl Shape { fn area(&self) -> int { 0 } }
1204 let start = @Point { x: 10f, y: 20f };
1205 let end = ~Point { x: (*start).x + 100f, y: (*start).y + 100f };
1206 let rect = &Rectangle(*start, *end);
1207 let area = (*rect).area();
1208 ~~~
1209
1210 To combat this ugliness the dot operator applies _automatic pointer
1211 dereferencing_ to the receiver (the value on the left-hand side of the
1212 dot), so in most cases, explicitly dereferencing the receiver is not necessary.
1213
1214 ~~~
1215 # struct Point { x: float, y: float }
1216 # enum Shape { Rectangle(Point, Point) }
1217 # impl Shape { fn area(&self) -> int { 0 } }
1218 let start = @Point { x: 10f, y: 20f };
1219 let end = ~Point { x: start.x + 100f, y: start.y + 100f };
1220 let rect = &Rectangle(*start, *end);
1221 let area = rect.area();
1222 ~~~
1223
1224 You can write an expression that dereferences any number of pointers
1225 automatically. For example, if you feel inclined, you could write
1226 something silly like
1227
1228 ~~~
1229 # struct Point { x: float, y: float }
1230 let point = &@~Point { x: 10f, y: 20f };
1231 println(fmt!("%f", point.x));
1232 ~~~
1233
1234 The indexing operator (`[]`) also auto-dereferences.
1235
1236 # Vectors and strings
1237
1238 A vector is a contiguous section of memory containing zero or more
1239 values of the same type. Like other types in Rust, vectors can be
1240 stored on the stack, the local heap, or the exchange heap. Borrowed
1241 pointers to vectors are also called 'slices'.
1242
1243 ~~~
1244 # enum Crayon {
1245 #     Almond, AntiqueBrass, Apricot,
1246 #     Aquamarine, Asparagus, AtomicTangerine,
1247 #     BananaMania, Beaver, Bittersweet,
1248 #     Black, BlizzardBlue, Blue
1249 # }
1250 // A fixed-size stack vector
1251 let stack_crayons: [Crayon, ..3] = [Almond, AntiqueBrass, Apricot];
1252
1253 // A borrowed pointer to stack-allocated vector
1254 let stack_crayons: &[Crayon] = &[Aquamarine, Asparagus, AtomicTangerine];
1255
1256 // A local heap (managed) vector of crayons
1257 let local_crayons: @[Crayon] = @[BananaMania, Beaver, Bittersweet];
1258
1259 // An exchange heap (owned) vector of crayons
1260 let exchange_crayons: ~[Crayon] = ~[Black, BlizzardBlue, Blue];
1261 ~~~
1262
1263 The `+` operator means concatenation when applied to vector types.
1264
1265 ~~~~
1266 # enum Crayon { Almond, AntiqueBrass, Apricot,
1267 #               Aquamarine, Asparagus, AtomicTangerine,
1268 #               BananaMania, Beaver, Bittersweet };
1269 # impl Clone for Crayon {
1270 #     fn clone(&self) -> Crayon {
1271 #         *self
1272 #     }
1273 # }
1274
1275 let my_crayons = ~[Almond, AntiqueBrass, Apricot];
1276 let your_crayons = ~[BananaMania, Beaver, Bittersweet];
1277
1278 // Add two vectors to create a new one
1279 let our_crayons = my_crayons + your_crayons;
1280
1281 // .push_all() will append to a vector, provided it lives in a mutable slot
1282 let mut my_crayons = my_crayons;
1283 my_crayons.push_all(your_crayons);
1284 ~~~~
1285
1286 > ***Note:*** The above examples of vector addition use owned
1287 > vectors. Some operations on slices and stack vectors are
1288 > not yet well-supported. Owned vectors are often the most
1289 > usable.
1290
1291 Square brackets denote indexing into a vector:
1292
1293 ~~~~
1294 # enum Crayon { Almond, AntiqueBrass, Apricot,
1295 #               Aquamarine, Asparagus, AtomicTangerine,
1296 #               BananaMania, Beaver, Bittersweet };
1297 # fn draw_scene(c: Crayon) { }
1298 let crayons: [Crayon, ..3] = [BananaMania, Beaver, Bittersweet];
1299 match crayons[0] {
1300     Bittersweet => draw_scene(crayons[0]),
1301     _ => ()
1302 }
1303 ~~~~
1304
1305 A vector can be destructured using pattern matching:
1306
1307 ~~~~
1308 let numbers: [int, ..3] = [1, 2, 3];
1309 let score = match numbers {
1310     [] => 0,
1311     [a] => a * 10,
1312     [a, b] => a * 6 + b * 4,
1313     [a, b, c, ..rest] => a * 5 + b * 3 + c * 2 + rest.len() as int
1314 };
1315 ~~~~
1316
1317 The elements of a vector _inherit the mutability of the vector_,
1318 and as such, individual elements may not be reassigned when the
1319 vector lives in an immutable slot.
1320
1321 ~~~ {.xfail-test}
1322 # enum Crayon { Almond, AntiqueBrass, Apricot,
1323 #               Aquamarine, Asparagus, AtomicTangerine,
1324 #               BananaMania, Beaver, Bittersweet };
1325 let crayons: ~[Crayon] = ~[BananaMania, Beaver, Bittersweet];
1326
1327 crayons[0] = Apricot; // ERROR: Can't assign to immutable vector
1328 ~~~
1329
1330 Moving it into a mutable slot makes the elements assignable.
1331
1332 ~~~
1333 # enum Crayon { Almond, AntiqueBrass, Apricot,
1334 #               Aquamarine, Asparagus, AtomicTangerine,
1335 #               BananaMania, Beaver, Bittersweet };
1336 let crayons: ~[Crayon] = ~[BananaMania, Beaver, Bittersweet];
1337
1338 // Put the vector into a mutable slot
1339 let mut mutable_crayons = crayons;
1340
1341 // Now it's mutable to the bone
1342 mutable_crayons[0] = Apricot;
1343 ~~~
1344
1345 This is a simple example of Rust's _dual-mode data structures_, also
1346 referred to as _freezing and thawing_.
1347
1348 Strings are implemented with vectors of `u8`, though they have a
1349 distinct type. They support most of the same allocation options as
1350 vectors, though the string literal without a storage sigil (for
1351 example, `"foo"`) is treated differently than a comparable vector
1352 (`[foo]`).  Whereas plain vectors are stack-allocated fixed-length
1353 vectors, plain strings are borrowed pointers to read-only (static)
1354 memory. All strings are immutable.
1355
1356 ~~~
1357 // A plain string is a slice to read-only (static) memory
1358 let stack_crayons: &str = "Almond, AntiqueBrass, Apricot";
1359
1360 // The same thing, but with the `&`
1361 let stack_crayons: &str = &"Aquamarine, Asparagus, AtomicTangerine";
1362
1363 // A local heap (managed) string
1364 let local_crayons: @str = @"BananaMania, Beaver, Bittersweet";
1365
1366 // An exchange heap (owned) string
1367 let exchange_crayons: ~str = ~"Black, BlizzardBlue, Blue";
1368 ~~~
1369
1370 Both vectors and strings support a number of useful
1371 [methods](#methods), defined in [`std::vec`]
1372 and [`std::str`]. Here are some examples.
1373
1374 [`std::vec`]: std/vec.html
1375 [`std::str`]: std/str.html
1376
1377 ~~~
1378 # enum Crayon {
1379 #     Almond, AntiqueBrass, Apricot,
1380 #     Aquamarine, Asparagus, AtomicTangerine,
1381 #     BananaMania, Beaver, Bittersweet
1382 # }
1383 # fn unwrap_crayon(c: Crayon) -> int { 0 }
1384 # fn eat_crayon_wax(i: int) { }
1385 # fn store_crayon_in_nasal_cavity(i: uint, c: Crayon) { }
1386 # fn crayon_to_str(c: Crayon) -> &str { "" }
1387
1388 let crayons = [Almond, AntiqueBrass, Apricot];
1389
1390 // Check the length of the vector
1391 assert!(crayons.len() == 3);
1392 assert!(!crayons.is_empty());
1393
1394 // Iterate over a vector, obtaining a pointer to each element
1395 // (`for` is explained in the container/iterator tutorial)
1396 for crayon in crayons.iter() {
1397     let delicious_crayon_wax = unwrap_crayon(*crayon);
1398     eat_crayon_wax(delicious_crayon_wax);
1399 }
1400
1401 // Map vector elements
1402 let crayon_names = crayons.map(|v| crayon_to_str(*v));
1403 let favorite_crayon_name = crayon_names[0];
1404
1405 // Remove whitespace from before and after the string
1406 let new_favorite_crayon_name = favorite_crayon_name.trim();
1407
1408 if favorite_crayon_name.len() > 5 {
1409    // Create a substring
1410    println(favorite_crayon_name.slice_chars(0, 5));
1411 }
1412 ~~~
1413
1414 # Closures
1415
1416 Named functions, like those we've seen so far, may not refer to local
1417 variables declared outside the function: they do not close over their
1418 environment (sometimes referred to as "capturing" variables in their
1419 environment). For example, you couldn't write the following:
1420
1421 ~~~~ {.ignore}
1422 let foo = 10;
1423
1424 fn bar() -> int {
1425    return foo; // `bar` cannot refer to `foo`
1426 }
1427 ~~~~
1428
1429 Rust also supports _closures_, functions that can access variables in
1430 the enclosing scope.
1431
1432 ~~~~
1433 fn call_closure_with_ten(b: &fn(int)) { b(10); }
1434
1435 let captured_var = 20;
1436 let closure = |arg| println(fmt!("captured_var=%d, arg=%d", captured_var, arg));
1437
1438 call_closure_with_ten(closure);
1439 ~~~~
1440
1441 Closures begin with the argument list between vertical bars and are followed by
1442 a single expression. Remember that a block, `{ <expr1>; <expr2>; ... }`, is
1443 considered a single expression: it evaluates to the result of the last
1444 expression it contains if that expression is not followed by a semicolon,
1445 otherwise the block evaluates to `()`.
1446
1447 The types of the arguments are generally omitted, as is the return type,
1448 because the compiler can almost always infer them. In the rare case where the
1449 compiler needs assistance, though, the arguments and return types may be
1450 annotated.
1451
1452 ~~~~
1453 let square = |x: int| -> uint { (x * x) as uint };
1454 ~~~~
1455
1456 There are several forms of closure, each with its own role. The most
1457 common, called a _stack closure_, has type `&fn` and can directly
1458 access local variables in the enclosing scope.
1459
1460 ~~~~
1461 let mut max = 0;
1462 [1, 2, 3].map(|x| if *x > max { max = *x });
1463 ~~~~
1464
1465 Stack closures are very efficient because their environment is
1466 allocated on the call stack and refers by pointer to captured
1467 locals. To ensure that stack closures never outlive the local
1468 variables to which they refer, stack closures are not
1469 first-class. That is, they can only be used in argument position; they
1470 cannot be stored in data structures or returned from
1471 functions. Despite these limitations, stack closures are used
1472 pervasively in Rust code.
1473
1474 ## Managed closures
1475
1476 When you need to store a closure in a data structure, a stack closure
1477 will not do, since the compiler will refuse to let you store it. For
1478 this purpose, Rust provides a type of closure that has an arbitrary
1479 lifetime, written `@fn` (boxed closure, analogous to the `@` pointer
1480 type described earlier). This type of closure *is* first-class.
1481
1482 A managed closure does not directly access its environment, but merely
1483 copies out the values that it closes over into a private data
1484 structure. This means that it can not assign to these variables, and
1485 cannot observe updates to them.
1486
1487 This code creates a closure that adds a given string to its argument,
1488 returns it from a function, and then calls it:
1489
1490 ~~~~
1491 fn mk_appender(suffix: ~str) -> @fn(~str) -> ~str {
1492     // The compiler knows that we intend this closure to be of type @fn
1493     return |s| s + suffix;
1494 }
1495
1496 fn main() {
1497     let shout = mk_appender(~"!");
1498     println(shout(~"hey ho, let's go"));
1499 }
1500 ~~~~
1501
1502 ## Owned closures
1503
1504 Owned closures, written `~fn` in analogy to the `~` pointer type,
1505 hold on to things that can safely be sent between
1506 processes. They copy the values they close over, much like managed
1507 closures, but they also own them: that is, no other code can access
1508 them. Owned closures are used in concurrent code, particularly
1509 for spawning [tasks][tasks].
1510
1511 ## Closure compatibility
1512
1513 Rust closures have a convenient subtyping property: you can pass any kind of
1514 closure (as long as the arguments and return types match) to functions
1515 that expect a `&fn()`. Thus, when writing a higher-order function that
1516 only calls its function argument, and does nothing else with it, you
1517 should almost always declare the type of that argument as `&fn()`. That way,
1518 callers may pass any kind of closure.
1519
1520 ~~~~
1521 fn call_twice(f: &fn()) { f(); f(); }
1522 let closure = || { "I'm a closure, and it doesn't matter what type I am"; };
1523 fn function() { "I'm a normal function"; }
1524 call_twice(closure);
1525 call_twice(function);
1526 ~~~~
1527
1528 > ***Note:*** Both the syntax and the semantics will be changing
1529 > in small ways. At the moment they can be unsound in some
1530 > scenarios, particularly with non-copyable types.
1531
1532 ## Do syntax
1533
1534 The `do` expression provides a way to treat higher-order functions
1535 (functions that take closures as arguments) as control structures.
1536
1537 Consider this function that iterates over a vector of
1538 integers, passing in a pointer to each integer in the vector:
1539
1540 ~~~~
1541 fn each(v: &[int], op: &fn(v: &int)) {
1542    let mut n = 0;
1543    while n < v.len() {
1544        op(&v[n]);
1545        n += 1;
1546    }
1547 }
1548 ~~~~
1549
1550 As a caller, if we use a closure to provide the final operator
1551 argument, we can write it in a way that has a pleasant, block-like
1552 structure.
1553
1554 ~~~~
1555 # fn each(v: &[int], op: &fn(v: &int)) { }
1556 # fn do_some_work(i: &int) { }
1557 each([1, 2, 3], |n| {
1558     do_some_work(n);
1559 });
1560 ~~~~
1561
1562 This is such a useful pattern that Rust has a special form of function
1563 call that can be written more like a built-in control structure:
1564
1565 ~~~~
1566 # fn each(v: &[int], op: &fn(v: &int)) { }
1567 # fn do_some_work(i: &int) { }
1568 do each([1, 2, 3]) |n| {
1569     do_some_work(n);
1570 }
1571 ~~~~
1572
1573 The call is prefixed with the keyword `do` and, instead of writing the
1574 final closure inside the argument list, it appears outside of the
1575 parentheses, where it looks more like a typical block of
1576 code.
1577
1578 `do` is a convenient way to create tasks with the `task::spawn`
1579 function.  `spawn` has the signature `spawn(fn: ~fn())`. In other
1580 words, it is a function that takes an owned closure that takes no
1581 arguments.
1582
1583 ~~~~
1584 use std::task::spawn;
1585
1586 do spawn() || {
1587     debug!("I'm a task, whatever");
1588 }
1589 ~~~~
1590
1591 Look at all those bars and parentheses -- that's two empty argument
1592 lists back to back. Since that is so unsightly, empty argument lists
1593 may be omitted from `do` expressions.
1594
1595 ~~~~
1596 use std::task::spawn;
1597
1598 do spawn {
1599    debug!("Kablam!");
1600 }
1601 ~~~~
1602
1603 If you want to see the output of `debug!` statements, you will need to turn on `debug!` logging.
1604 To enable `debug!` logging, set the RUST_LOG environment variable to the name of your crate, which, for a file named `foo.rs`, will be `foo` (e.g., with bash, `export RUST_LOG=foo`).
1605
1606 # Methods
1607
1608 Methods are like functions except that they always begin with a special argument,
1609 called `self`,
1610 which has the type of the method's receiver. The
1611 `self` argument is like `this` in C++ and many other languages.
1612 Methods are called with dot notation, as in `my_vec.len()`.
1613
1614 _Implementations_, written with the `impl` keyword, can define
1615 methods on most Rust types, including structs and enums.
1616 As an example, let's define a `draw` method on our `Shape` enum.
1617
1618 ~~~
1619 # fn draw_circle(p: Point, f: float) { }
1620 # fn draw_rectangle(p: Point, p: Point) { }
1621 struct Point {
1622     x: float,
1623     y: float
1624 }
1625
1626 enum Shape {
1627     Circle(Point, float),
1628     Rectangle(Point, Point)
1629 }
1630
1631 impl Shape {
1632     fn draw(&self) {
1633         match *self {
1634             Circle(p, f) => draw_circle(p, f),
1635             Rectangle(p1, p2) => draw_rectangle(p1, p2)
1636         }
1637     }
1638 }
1639
1640 let s = Circle(Point { x: 1f, y: 2f }, 3f);
1641 s.draw();
1642 ~~~
1643
1644 This defines an _implementation_ for `Shape` containing a single
1645 method, `draw`. In most respects the `draw` method is defined
1646 like any other function, except for the name `self`.
1647
1648 The type of `self` is the type on which the method is implemented,
1649 or a pointer thereof. As an argument it is written either `self`,
1650 `&self`, `@self`, or `~self`.
1651 A caller must in turn have a compatible pointer type to call the method.
1652
1653 ~~~
1654 # fn draw_circle(p: Point, f: float) { }
1655 # fn draw_rectangle(p: Point, p: Point) { }
1656 # struct Point { x: float, y: float }
1657 # enum Shape {
1658 #     Circle(Point, float),
1659 #     Rectangle(Point, Point)
1660 # }
1661 impl Shape {
1662     fn draw_borrowed(&self) { ... }
1663     fn draw_managed(@self) { ... }
1664     fn draw_owned(~self) { ... }
1665     fn draw_value(self) { ... }
1666 }
1667
1668 let s = Circle(Point { x: 1f, y: 2f }, 3f);
1669
1670 (@s).draw_managed();
1671 (~s).draw_owned();
1672 (&s).draw_borrowed();
1673 s.draw_value();
1674 ~~~
1675
1676 Methods typically take a borrowed pointer self type,
1677 so the compiler will go to great lengths to convert a callee
1678 to a borrowed pointer.
1679
1680 ~~~
1681 # fn draw_circle(p: Point, f: float) { }
1682 # fn draw_rectangle(p: Point, p: Point) { }
1683 # struct Point { x: float, y: float }
1684 # enum Shape {
1685 #     Circle(Point, float),
1686 #     Rectangle(Point, Point)
1687 # }
1688 # impl Shape {
1689 #    fn draw_borrowed(&self) { ... }
1690 #    fn draw_managed(@self) { ... }
1691 #    fn draw_owned(~self) { ... }
1692 #    fn draw_value(self) { ... }
1693 # }
1694 # let s = Circle(Point { x: 1f, y: 2f }, 3f);
1695 // As with typical function arguments, managed and owned pointers
1696 // are automatically converted to borrowed pointers
1697
1698 (@s).draw_borrowed();
1699 (~s).draw_borrowed();
1700
1701 // Unlike typical function arguments, the self value will
1702 // automatically be referenced ...
1703 s.draw_borrowed();
1704
1705 // ... and dereferenced
1706 (& &s).draw_borrowed();
1707
1708 // ... and dereferenced and borrowed
1709 (&@~s).draw_borrowed();
1710 ~~~
1711
1712 Implementations may also define standalone (sometimes called "static")
1713 methods. The absence of a `self` parameter distinguishes such methods.
1714 These methods are the preferred way to define constructor functions.
1715
1716 ~~~~ {.xfail-test}
1717 impl Circle {
1718     fn area(&self) -> float { ... }
1719     fn new(area: float) -> Circle { ... }
1720 }
1721 ~~~~
1722
1723 To call such a method, just prefix it with the type name and a double colon:
1724
1725 ~~~~
1726 use std::float::consts::pi;
1727 struct Circle { radius: float }
1728 impl Circle {
1729     fn new(area: float) -> Circle { Circle { radius: (area / pi).sqrt() } }
1730 }
1731 let c = Circle::new(42.5);
1732 ~~~~
1733
1734 # Generics
1735
1736 Throughout this tutorial, we've been defining functions that act only
1737 on specific data types. With type parameters we can also define
1738 functions whose arguments have generic types, and which can be invoked
1739 with a variety of types. Consider a generic `map` function, which
1740 takes a function `function` and a vector `vector` and returns a new
1741 vector consisting of the result of applying `function` to each element
1742 of `vector`:
1743
1744 ~~~~
1745 fn map<T, U>(vector: &[T], function: &fn(v: &T) -> U) -> ~[U] {
1746     let mut accumulator = ~[];
1747     for element in vector.iter() {
1748         accumulator.push(function(element));
1749     }
1750     return accumulator;
1751 }
1752 ~~~~
1753
1754 When defined with type parameters, as denoted by `<T, U>`, this
1755 function can be applied to any type of vector, as long as the type of
1756 `function`'s argument and the type of the vector's contents agree with
1757 each other.
1758
1759 Inside a generic function, the names of the type parameters
1760 (capitalized by convention) stand for opaque types. All you can do
1761 with instances of these types is pass them around: you can't apply any
1762 operations to them or pattern-match on them. Note that instances of
1763 generic types are often passed by pointer. For example, the parameter
1764 `function()` is supplied with a pointer to a value of type `T` and not
1765 a value of type `T` itself. This ensures that the function works with
1766 the broadest set of types possible, since some types are expensive or
1767 illegal to copy and pass by value.
1768
1769 Generic `type`, `struct`, and `enum` declarations follow the same pattern:
1770
1771 ~~~~
1772 use std::hashmap::HashMap;
1773 type Set<T> = HashMap<T, ()>;
1774
1775 struct Stack<T> {
1776     elements: ~[T]
1777 }
1778
1779 enum Option<T> {
1780     Some(T),
1781     None
1782 }
1783 ~~~~
1784
1785 These declarations can be instantiated to valid types like `Set<int>`,
1786 `Stack<int>`, and `Option<int>`.
1787
1788 The last type in that example, `Option`, appears frequently in Rust code.
1789 Because Rust does not have null pointers (except in unsafe code), we need
1790 another way to write a function whose result isn't defined on every possible
1791 combination of arguments of the appropriate types. The usual way is to write
1792 a function that returns `Option<T>` instead of `T`.
1793
1794 ~~~~
1795 # struct Point { x: float, y: float }
1796 # enum Shape { Circle(Point, float), Rectangle(Point, Point) }
1797 fn radius(shape: Shape) -> Option<float> {
1798     match shape {
1799         Circle(_, radius) => Some(radius),
1800         Rectangle(*)      => None
1801     }
1802 }
1803 ~~~~
1804
1805 The Rust compiler compiles generic functions very efficiently by
1806 *monomorphizing* them. *Monomorphization* is a fancy name for a simple
1807 idea: generate a separate copy of each generic function at each call site,
1808 a copy that is specialized to the argument
1809 types and can thus be optimized specifically for them. In this
1810 respect, Rust's generics have similar performance characteristics to
1811 C++ templates.
1812
1813 ## Traits
1814
1815 Within a generic function the operations available on generic types
1816 are very limited. After all, since the function doesn't know what
1817 types it is operating on, it can't safely modify or query their
1818 values. This is where _traits_ come into play. Traits are Rust's most
1819 powerful tool for writing polymorphic code. Java developers will see
1820 them as similar to Java interfaces, and Haskellers will notice their
1821 similarities to type classes. Rust's traits are a form of *bounded
1822 polymorphism*: a trait is a way of limiting the set of possible types
1823 that a type parameter could refer to.
1824
1825 As motivation, let us consider copying in Rust.
1826 The `clone` method is not defined for all Rust types.
1827 One reason is user-defined destructors:
1828 copying a type that has a destructor
1829 could result in the destructor running multiple times.
1830 Therefore, types with destructors cannot be copied
1831 unless you explicitly implement `Clone` for them.
1832
1833 This complicates handling of generic functions.
1834 If you have a type parameter `T`, can you copy values of that type?
1835 In Rust, you can't,
1836 and if you try to run the following code the compiler will complain.
1837
1838 ~~~~ {.xfail-test}
1839 // This does not compile
1840 fn head_bad<T>(v: &[T]) -> T {
1841     v[0] // error: copying a non-copyable value
1842 }
1843 ~~~~
1844
1845 However, we can tell the compiler
1846 that the `head` function is only for copyable types:
1847 that is, those that implement the `Clone` trait.
1848 In that case,
1849 we can explicitly create a second copy of the value we are returning
1850 using the `clone` keyword:
1851
1852 ~~~~
1853 // This does
1854 fn head<T: Clone>(v: &[T]) -> T {
1855     v[0].clone()
1856 }
1857 ~~~~
1858
1859 This says that we can call `head` on any type `T`
1860 as long as that type implements the `Clone` trait.
1861 When instantiating a generic function,
1862 you can only instantiate it with types
1863 that implement the correct trait,
1864 so you could not apply `head` to a type
1865 that does not implement `Clone`.
1866
1867 While most traits can be defined and implemented by user code,
1868 two traits are automatically derived and implemented
1869 for all applicable types by the compiler,
1870 and may not be overridden:
1871
1872 * `Send` - Sendable types.
1873 Types are sendable
1874 unless they contain managed boxes, managed closures, or borrowed pointers.
1875
1876 * `Freeze` - Constant (immutable) types.
1877 These are types that do not contain anything intrinsically mutable.
1878 Intrinsically mutable values include `@mut`
1879 and `Cell` in the standard library.
1880
1881 > ***Note:*** These two traits were referred to as 'kinds' in earlier
1882 > iterations of the language, and often still are.
1883
1884 Additionally, the `Drop` trait is used to define destructors. This
1885 trait defines one method called `drop`, which is automatically
1886 called when a value of the type that implements this trait is
1887 destroyed, either because the value went out of scope or because the
1888 garbage collector reclaimed it.
1889
1890 ~~~
1891 struct TimeBomb {
1892     explosivity: uint
1893 }
1894
1895 impl Drop for TimeBomb {
1896     fn drop(&self) {
1897         do self.explosivity.times {
1898             println("blam!");
1899         }
1900     }
1901 }
1902 ~~~
1903
1904 It is illegal to call `drop` directly. Only code inserted by the compiler
1905 may call it.
1906
1907 ## Declaring and implementing traits
1908
1909 A trait consists of a set of methods without bodies,
1910 or may be empty, as is the case with `Send` and `Freeze`.
1911 For example, we could declare the trait
1912 `Printable` for things that can be printed to the console,
1913 with a single method:
1914
1915 ~~~~
1916 trait Printable {
1917     fn print(&self);
1918 }
1919 ~~~~
1920
1921 Traits may be implemented for specific types with [impls]. An impl
1922 that implements a trait includes the name of the trait at the start of
1923 the definition, as in the following impls of `Printable` for `int`
1924 and `~str`.
1925
1926 [impls]: #methods
1927
1928 ~~~~
1929 # trait Printable { fn print(&self); }
1930 impl Printable for int {
1931     fn print(&self) { println(fmt!("%d", *self)) }
1932 }
1933
1934 impl Printable for ~str {
1935     fn print(&self) { println(*self) }
1936 }
1937
1938 # 1.print();
1939 # (~"foo").print();
1940 ~~~~
1941
1942 Methods defined in an implementation of a trait may be called just like
1943 any other method, using dot notation, as in `1.print()`. Traits may
1944 themselves contain type parameters. A trait for generalized sequence
1945 types might look like the following:
1946
1947 ~~~~
1948 trait Seq<T> {
1949     fn length(&self) -> uint;
1950 }
1951
1952 impl<T> Seq<T> for ~[T] {
1953     fn length(&self) -> uint { self.len() }
1954 }
1955 ~~~~
1956
1957 The implementation has to explicitly declare the type parameter that
1958 it binds, `T`, before using it to specify its trait type. Rust
1959 requires this declaration because the `impl` could also, for example,
1960 specify an implementation of `Seq<int>`. The trait type (appearing
1961 between `impl` and `for`) *refers* to a type, rather than
1962 defining one.
1963
1964 The type parameters bound by a trait are in scope in each of the
1965 method declarations. So, re-declaring the type parameter
1966 `T` as an explicit type parameter for `len`, in either the trait or
1967 the impl, would be a compile-time error.
1968
1969 Within a trait definition, `Self` is a special type that you can think
1970 of as a type parameter. An implementation of the trait for any given
1971 type `T` replaces the `Self` type parameter with `T`. The following
1972 trait describes types that support an equality operation:
1973
1974 ~~~~
1975 // In a trait, `self` refers to the self argument.
1976 // `Self` refers to the type implementing the trait.
1977 trait Eq {
1978     fn equals(&self, other: &Self) -> bool;
1979 }
1980
1981 // In an impl, `self` refers just to the value of the receiver
1982 impl Eq for int {
1983     fn equals(&self, other: &int) -> bool { *other == *self }
1984 }
1985 ~~~~
1986
1987 Notice that in the trait definition, `equals` takes a
1988 second parameter of type `Self`.
1989 In contrast, in the `impl`, `equals` takes a second parameter of
1990 type `int`, only using `self` as the name of the receiver.
1991
1992 Just as in type implementations, traits can define standalone (static)
1993 methods.  These methods are called by prefixing the method name with the trait
1994 name and a double colon.  The compiler uses type inference to decide which
1995 implementation to use.
1996
1997 ~~~~
1998 use std::float::consts::pi;
1999 trait Shape { fn new(area: float) -> Self; }
2000 struct Circle { radius: float }
2001 struct Square { length: float }
2002
2003 impl Shape for Circle {
2004     fn new(area: float) -> Circle { Circle { radius: (area / pi).sqrt() } }
2005 }
2006 impl Shape for Square {
2007     fn new(area: float) -> Square { Square { length: (area).sqrt() } }
2008 }
2009
2010 let area = 42.5;
2011 let c: Circle = Shape::new(area);
2012 let s: Square = Shape::new(area);
2013 ~~~~
2014
2015 ## Bounded type parameters and static method dispatch
2016
2017 Traits give us a language for defining predicates on types, or
2018 abstract properties that types can have. We can use this language to
2019 define _bounds_ on type parameters, so that we can then operate on
2020 generic types.
2021
2022 ~~~~
2023 # trait Printable { fn print(&self); }
2024 fn print_all<T: Printable>(printable_things: ~[T]) {
2025     for thing in printable_things.iter() {
2026         thing.print();
2027     }
2028 }
2029 ~~~~
2030
2031 Declaring `T` as conforming to the `Printable` trait (as we earlier
2032 did with `Clone`) makes it possible to call methods from that trait
2033 on values of type `T` inside the function. It will also cause a
2034 compile-time error when anyone tries to call `print_all` on an array
2035 whose element type does not have a `Printable` implementation.
2036
2037 Type parameters can have multiple bounds by separating them with `+`,
2038 as in this version of `print_all` that copies elements.
2039
2040 ~~~
2041 # trait Printable { fn print(&self); }
2042 fn print_all<T: Printable + Clone>(printable_things: ~[T]) {
2043     let mut i = 0;
2044     while i < printable_things.len() {
2045         let copy_of_thing = printable_things[i].clone();
2046         copy_of_thing.print();
2047         i += 1;
2048     }
2049 }
2050 ~~~
2051
2052 Method calls to bounded type parameters are _statically dispatched_,
2053 imposing no more overhead than normal function invocation, so are
2054 the preferred way to use traits polymorphically.
2055
2056 This usage of traits is similar to Haskell type classes.
2057
2058 ## Trait objects and dynamic method dispatch
2059
2060 The above allows us to define functions that polymorphically act on
2061 values of a single unknown type that conforms to a given trait.
2062 However, consider this function:
2063
2064 ~~~~
2065 # type Circle = int; type Rectangle = int;
2066 # impl Drawable for int { fn draw(&self) {} }
2067 # fn new_circle() -> int { 1 }
2068 trait Drawable { fn draw(&self); }
2069
2070 fn draw_all<T: Drawable>(shapes: ~[T]) {
2071     for shape in shapes.iter() { shape.draw(); }
2072 }
2073 # let c: Circle = new_circle();
2074 # draw_all(~[c]);
2075 ~~~~
2076
2077 You can call that on an array of circles, or an array of rectangles
2078 (assuming those have suitable `Drawable` traits defined), but not on
2079 an array containing both circles and rectangles. When such behavior is
2080 needed, a trait name can alternately be used as a type, called
2081 an _object_.
2082
2083 ~~~~
2084 # trait Drawable { fn draw(&self); }
2085 fn draw_all(shapes: &[@Drawable]) {
2086     for shape in shapes.iter() { shape.draw(); }
2087 }
2088 ~~~~
2089
2090 In this example, there is no type parameter. Instead, the `@Drawable`
2091 type denotes any managed box value that implements the `Drawable`
2092 trait. To construct such a value, you use the `as` operator to cast a
2093 value to an object:
2094
2095 ~~~~
2096 # type Circle = int; type Rectangle = bool;
2097 # trait Drawable { fn draw(&self); }
2098 # fn new_circle() -> Circle { 1 }
2099 # fn new_rectangle() -> Rectangle { true }
2100 # fn draw_all(shapes: &[@Drawable]) {}
2101
2102 impl Drawable for Circle { fn draw(&self) { ... } }
2103 impl Drawable for Rectangle { fn draw(&self) { ... } }
2104
2105 let c: @Circle = @new_circle();
2106 let r: @Rectangle = @new_rectangle();
2107 draw_all([c as @Drawable, r as @Drawable]);
2108 ~~~~
2109
2110 We omit the code for `new_circle` and `new_rectangle`; imagine that
2111 these just return `Circle`s and `Rectangle`s with a default size. Note
2112 that, like strings and vectors, objects have dynamic size and may
2113 only be referred to via one of the pointer types.
2114 Other pointer types work as well.
2115 Casts to traits may only be done with compatible pointers so,
2116 for example, an `@Circle` may not be cast to an `~Drawable`.
2117
2118 ~~~
2119 # type Circle = int; type Rectangle = int;
2120 # trait Drawable { fn draw(&self); }
2121 # impl Drawable for int { fn draw(&self) {} }
2122 # fn new_circle() -> int { 1 }
2123 # fn new_rectangle() -> int { 2 }
2124 // A managed object
2125 let boxy: @Drawable = @new_circle() as @Drawable;
2126 // An owned object
2127 let owny: ~Drawable = ~new_circle() as ~Drawable;
2128 // A borrowed object
2129 let stacky: &Drawable = &new_circle() as &Drawable;
2130 ~~~
2131
2132 Method calls to trait types are _dynamically dispatched_. Since the
2133 compiler doesn't know specifically which functions to call at compile
2134 time, it uses a lookup table (also known as a vtable or dictionary) to
2135 select the method to call at runtime.
2136
2137 This usage of traits is similar to Java interfaces.
2138
2139 ## Trait inheritance
2140
2141 We can write a trait declaration that _inherits_ from other traits, called _supertraits_.
2142 Types that implement a trait must also implement its supertraits.
2143 For example,
2144 we can define a `Circle` trait that inherits from `Shape`.
2145
2146 ~~~~
2147 trait Shape { fn area(&self) -> float; }
2148 trait Circle : Shape { fn radius(&self) -> float; }
2149 ~~~~
2150
2151 Now, we can implement `Circle` on a type only if we also implement `Shape`.
2152
2153 ~~~~
2154 use std::float::consts::pi;
2155 # trait Shape { fn area(&self) -> float; }
2156 # trait Circle : Shape { fn radius(&self) -> float; }
2157 # struct Point { x: float, y: float }
2158 # fn square(x: float) -> float { x * x }
2159 struct CircleStruct { center: Point, radius: float }
2160 impl Circle for CircleStruct {
2161     fn radius(&self) -> float { (self.area() / pi).sqrt() }
2162 }
2163 impl Shape for CircleStruct {
2164     fn area(&self) -> float { pi * square(self.radius) }
2165 }
2166 ~~~~
2167
2168 Notice that methods of `Circle` can call methods on `Shape`, as our
2169 `radius` implementation calls the `area` method.
2170 This is a silly way to compute the radius of a circle
2171 (since we could just return the `radius` field), but you get the idea.
2172
2173 In type-parameterized functions,
2174 methods of the supertrait may be called on values of subtrait-bound type parameters.
2175 Refering to the previous example of `trait Circle : Shape`:
2176
2177 ~~~
2178 # trait Shape { fn area(&self) -> float; }
2179 # trait Circle : Shape { fn radius(&self) -> float; }
2180 fn radius_times_area<T: Circle>(c: T) -> float {
2181     // `c` is both a Circle and a Shape
2182     c.radius() * c.area()
2183 }
2184 ~~~
2185
2186 Likewise, supertrait methods may also be called on trait objects.
2187
2188 ~~~ {.xfail-test}
2189 use std::float::consts::pi;
2190 # trait Shape { fn area(&self) -> float; }
2191 # trait Circle : Shape { fn radius(&self) -> float; }
2192 # struct Point { x: float, y: float }
2193 # struct CircleStruct { center: Point, radius: float }
2194 # impl Circle for CircleStruct { fn radius(&self) -> float { (self.area() / pi).sqrt() } }
2195 # impl Shape for CircleStruct { fn area(&self) -> float { pi * square(self.radius) } }
2196
2197 let concrete = @CircleStruct{center:Point{x:3f,y:4f},radius:5f};
2198 let mycircle: Circle = concrete as @Circle;
2199 let nonsense = mycircle.radius() * mycircle.area();
2200 ~~~
2201
2202 > ***Note:*** Trait inheritance does not actually work with objects yet
2203
2204 ## Deriving implementations for traits
2205
2206 A small number of traits in `std` and `extra` can have implementations
2207 that can be automatically derived. These instances are specified by
2208 placing the `deriving` attribute on a data type declaration. For
2209 example, the following will mean that `Circle` has an implementation
2210 for `Eq` and can be used with the equality operators, and that a value
2211 of type `ABC` can be randomly generated and converted to a string:
2212
2213 ~~~
2214 #[deriving(Eq)]
2215 struct Circle { radius: float }
2216
2217 #[deriving(Rand, ToStr)]
2218 enum ABC { A, B, C }
2219 ~~~
2220
2221 The full list of derivable traits is `Eq`, `TotalEq`, `Ord`,
2222 `TotalOrd`, `Encodable` `Decodable`, `Clone`, `DeepClone`,
2223 `IterBytes`, `Rand`, `Zero`, and `ToStr`.
2224
2225 # Modules and crates
2226
2227 The Rust namespace is arranged in a hierarchy of modules. Each source
2228 (.rs) file represents a single module and may in turn contain
2229 additional modules.
2230
2231 ~~~~
2232 mod farm {
2233     pub fn chicken() -> &str { "cluck cluck" }
2234     pub fn cow() -> &str { "mooo" }
2235 }
2236
2237 fn main() {
2238     println(farm::chicken());
2239 }
2240 ~~~~
2241
2242 The contents of modules can be imported into the current scope
2243 with the `use` keyword, optionally giving it an alias. `use`
2244 may appear at the beginning of crates, `mod`s, `fn`s, and other
2245 blocks.
2246
2247 ~~~
2248 # mod farm { pub fn chicken() { } }
2249 # fn main() {
2250 // Bring `chicken` into scope
2251 use farm::chicken;
2252
2253 fn chicken_farmer() {
2254     // The same, but name it `my_chicken`
2255     use my_chicken = farm::chicken;
2256     ...
2257 # my_chicken();
2258 }
2259 # chicken();
2260 # }
2261 ~~~
2262
2263 These farm animal functions have a new keyword, `pub`, attached to
2264 them. The `pub` keyword modifies an item's visibility, making it
2265 visible outside its containing module. An expression with `::`, like
2266 `farm::chicken`, can name an item outside of its containing
2267 module. Items, such as those declared with `fn`, `struct`, `enum`,
2268 `type`, or `static`, are module-private by default.
2269
2270 Visibility restrictions in Rust exist only at module boundaries. This
2271 is quite different from most object-oriented languages that also
2272 enforce restrictions on objects themselves. That's not to say that
2273 Rust doesn't support encapsulation: both struct fields and methods can
2274 be private. But this encapsulation is at the module level, not the
2275 struct level. Note that fields and methods are _public_ by default.
2276
2277 ~~~
2278 pub mod farm {
2279 # pub type Chicken = int;
2280 # type Cow = int;
2281 # struct Human(int);
2282 # impl Human { fn rest(&self) { } }
2283 # pub fn make_me_a_farm() -> Farm { Farm { chickens: ~[], cows: ~[], farmer: Human(0) } }
2284     pub struct Farm {
2285         priv chickens: ~[Chicken],
2286         priv cows: ~[Cow],
2287         farmer: Human
2288     }
2289
2290     impl Farm {
2291         priv fn feed_chickens(&self) { ... }
2292         priv fn feed_cows(&self) { ... }
2293         pub fn add_chicken(&self, c: Chicken) { ... }
2294     }
2295
2296     pub fn feed_animals(farm: &Farm) {
2297         farm.feed_chickens();
2298         farm.feed_cows();
2299     }
2300 }
2301
2302 fn main() {
2303      let f = make_me_a_farm();
2304      f.add_chicken(make_me_a_chicken());
2305      farm::feed_animals(&f);
2306      f.farmer.rest();
2307 }
2308 # fn make_me_a_farm() -> farm::Farm { farm::make_me_a_farm() }
2309 # fn make_me_a_chicken() -> farm::Chicken { 0 }
2310 ~~~
2311
2312 ## Crates
2313
2314 The unit of independent compilation in Rust is the crate: rustc
2315 compiles a single crate at a time, from which it produces either a
2316 library or an executable.
2317
2318 When compiling a single `.rs` source file, the file acts as the whole crate.
2319 You can compile it with the `--lib` compiler switch to create a shared
2320 library, or without, provided that your file contains a `fn main`
2321 somewhere, to create an executable.
2322
2323 Larger crates typically span multiple files and are, by convention,
2324 compiled from a source file with the `.rc` extension, called a *crate file*.
2325 The crate file extension distinguishes source files that represent
2326 crates from those that do not, but otherwise source files and crate files are identical.
2327
2328 A typical crate file declares attributes associated with the crate that
2329 may affect how the compiler processes the source.
2330 Crate attributes specify metadata used for locating and linking crates,
2331 the type of crate (library or executable),
2332 and control warning and error behavior,
2333 among other things.
2334 Crate files additionally declare the external crates they depend on
2335 as well as any modules loaded from other files.
2336
2337 ~~~~ { .xfail-test }
2338 // Crate linkage metadata
2339 #[link(name = "farm", vers = "2.5", author = "mjh")];
2340
2341 // Make a library ("bin" is the default)
2342 #[crate_type = "lib"];
2343
2344 // Turn on a warning
2345 #[warn(non_camel_case_types)]
2346
2347 // Link to the standard library
2348 extern mod std;
2349
2350 // Load some modules from other files
2351 mod cow;
2352 mod chicken;
2353 mod horse;
2354
2355 fn main() {
2356     ...
2357 }
2358 ~~~~
2359
2360 Compiling this file will cause `rustc` to look for files named
2361 `cow.rs`, `chicken.rs`, and `horse.rs` in the same directory as the
2362 `.rc` file, compile them all together, and, based on the presence of
2363 the `crate_type = "lib"` attribute, output a shared library or an
2364 executable. (If the line `#[crate_type = "lib"];` was omitted,
2365 `rustc` would create an executable.)
2366
2367 The `#[link(...)]` attribute provides meta information about the
2368 module, which other crates can use to load the right module. More
2369 about that later.
2370
2371 To have a nested directory structure for your source files, you can
2372 nest mods:
2373
2374 ~~~~ {.ignore}
2375 mod poultry {
2376     mod chicken;
2377     mod turkey;
2378 }
2379 ~~~~
2380
2381 The compiler will now look for `poultry/chicken.rs` and
2382 `poultry/turkey.rs`, and export their content in `poultry::chicken`
2383 and `poultry::turkey`. You can also provide a `poultry.rs` to add
2384 content to the `poultry` module itself.
2385
2386 ## Using other crates
2387
2388 The `extern mod` directive lets you use a crate (once it's been
2389 compiled into a library) from inside another crate. `extern mod` can
2390 appear at the top of a crate file or at the top of modules. It will
2391 cause the compiler to look in the library search path (which you can
2392 extend with the `-L` switch) for a compiled Rust library with the
2393 right name, then add a module with that crate's name into the local
2394 scope.
2395
2396 For example, `extern mod std` links the [standard library].
2397
2398 [standard library]: std/index.html
2399
2400 When a comma-separated list of name/value pairs appears after `extern
2401 mod`, the compiler front-end matches these pairs against the
2402 attributes provided in the `link` attribute of the crate file. The
2403 front-end will only select this crate for use if the actual pairs
2404 match the declared attributes. You can provide a `name` value to
2405 override the name used to search for the crate.
2406
2407 Our example crate declared this set of `link` attributes:
2408
2409 ~~~~
2410 #[link(name = "farm", vers = "2.5", author = "mjh")];
2411 ~~~~
2412
2413 Which you can then link with any (or all) of the following:
2414
2415 ~~~~ {.xfail-test}
2416 extern mod farm;
2417 extern mod my_farm (name = "farm", vers = "2.5");
2418 extern mod my_auxiliary_farm (name = "farm", author = "mjh");
2419 ~~~~
2420
2421 If any of the requested metadata do not match, then the crate
2422 will not be compiled successfully.
2423
2424 ## A minimal example
2425
2426 Now for something that you can actually compile yourself, we have
2427 these two files:
2428
2429 ~~~~
2430 // world.rs
2431 #[link(name = "world", vers = "1.0")];
2432 pub fn explore() -> &str { "world" }
2433 ~~~~
2434
2435 ~~~~ {.xfail-test}
2436 // main.rs
2437 extern mod world;
2438 fn main() { println(~"hello " + world::explore()); }
2439 ~~~~
2440
2441 Now compile and run like this (adjust to your platform if necessary):
2442
2443 ~~~~ {.notrust}
2444 > rustc --lib world.rs  # compiles libworld-94839cbfe144198-1.0.so
2445 > rustc main.rs -L .    # compiles main
2446 > ./main
2447 "hello world"
2448 ~~~~
2449
2450 Notice that the library produced contains the version in the filename
2451 as well as an inscrutable string of alphanumerics. These are both
2452 part of Rust's library versioning scheme. The alphanumerics are
2453 a hash representing the crate metadata.
2454
2455 ## The standard library
2456
2457 The Rust standard library provides runtime features required by the language,
2458 including the task scheduler and memory allocators, as well as library
2459 support for Rust built-in types, platform abstractions, and other commonly
2460 used features.
2461
2462 [`std`] includes modules corresponding to each of the integer types, each of
2463 the floating point types, the [`bool`] type, [tuples], [characters], [strings],
2464 [vectors], [managed boxes], [owned boxes],
2465 and unsafe and borrowed [pointers].  Additionally, `std` provides
2466 some pervasive types ([`option`] and [`result`]),
2467 [task] creation and [communication] primitives,
2468 platform abstractions ([`os`] and [`path`]), basic
2469 I/O abstractions ([`io`]), [containers] like [`hashmap`],
2470 common traits ([`kinds`], [`ops`], [`cmp`], [`num`],
2471 [`to_str`], [`clone`]), and complete bindings to the C standard library ([`libc`]).
2472
2473 ### Standard Library injection and the Rust prelude
2474
2475 `std` is imported at the topmost level of every crate by default, as
2476 if the first line of each crate was
2477
2478     extern mod std;
2479
2480 This means that the contents of std can be accessed from from any context
2481 with the `std::` path prefix, as in `use std::vec`, `use std::task::spawn`,
2482 etc.
2483
2484 Additionally, `std` contains a `prelude` module that reexports many of the
2485 most common standard modules, types and traits. The contents of the prelude are
2486 imported into every *module* by default.  Implicitly, all modules behave as if
2487 they contained the following prologue:
2488
2489     use std::prelude::*;
2490
2491 [`std`]: std/index.html
2492 [`bool`]: std/bool.html
2493 [tuples]: std/tuple.html
2494 [characters]: std/char.html
2495 [strings]: std/str.html
2496 [vectors]: std/vec.html
2497 [managed boxes]: std/managed.html
2498 [owned boxes]: std/owned.html
2499 [pointers]: std/ptr.html
2500 [`option`]: std/option.html
2501 [`result`]: std/result.html
2502 [task]: std/task.html
2503 [communication]: std/comm.html
2504 [`os`]: std/os.html
2505 [`path`]: std/path.html
2506 [`io`]: std/io.html
2507 [containers]: std/container.html
2508 [`hashmap`]: std/hashmap.html
2509 [`kinds`]: std/kinds.html
2510 [`ops`]: std/ops.html
2511 [`cmp`]: std/cmp.html
2512 [`num`]: std/num.html
2513 [`to_str`]: std/to_str.html
2514 [`clone`]: std/clone.html
2515 [`libc`]: std/libc.html
2516
2517 # What next?
2518
2519 Now that you know the essentials, check out any of the additional
2520 tutorials on individual topics.
2521
2522 * [Borrowed pointers][borrow]
2523 * [Tasks and communication][tasks]
2524 * [Macros][macros]
2525 * [The foreign function interface][ffi]
2526 * [Containers and iterators](tutorial-container.html)
2527
2528 There is further documentation on the [wiki].
2529
2530 [borrow]: tutorial-borrowed-ptr.html
2531 [tasks]: tutorial-tasks.html
2532 [macros]: tutorial-macros.html
2533 [ffi]: tutorial-ffi.html
2534
2535 [wiki]: https://github.com/mozilla/rust/wiki/Docs
2536