@@ -716,8 +716,8 @@ When an enum has simple integer discriminators, you can apply the `as` cast
716716operator to convert a variant to its discriminator value as an ` int ` :
717717
718718~~~~
719- # #[deriving(Show)] enum Direction { North }
720- println!( "{} => {}", North , North as int );
719+ # enum Direction { North, East, South, West }
720+ println!( "North => {}", North as int );
721721~~~~
722722
723723It is possible to set the discriminator values to chosen constant values:
@@ -748,15 +748,23 @@ includes an identifier of the actual form that it holds, much like the
748748
749749This declaration defines a type ` Shape ` that can refer to such shapes, and two
750750functions, ` Circle ` and ` Rectangle ` , which can be used to construct values of
751- the type. To create a new Circle, write `Circle(Point { x: 0.0, y: 0.0 },
752- 10.0)`.
751+ the type.
752+
753+ To create a new ` Circle ` , write:
754+
755+ ~~~~
756+ # struct Point { x: f64, y: f64 }
757+ # enum Shape { Circle(Point, f64), Rectangle(Point, Point) }
758+ let circle = Circle(Point { x: 0.0, y: 0.0 }, 10.0);
759+ ~~~~
753760
754761All of these variant constructors may be used as patterns. The only way to
755762access the contents of an enum instance is the destructuring of a match. For
756763example:
757764
758765~~~~
759766use std::f64;
767+
760768# struct Point {x: f64, y: f64}
761769# enum Shape { Circle(Point, f64), Rectangle(Point, Point) }
762770fn area(sh: Shape) -> f64 {
@@ -765,6 +773,9 @@ fn area(sh: Shape) -> f64 {
765773 Rectangle(Point { x, y }, Point { x: x2, y: y2 }) => (x2 - x) * (y2 - y)
766774 }
767775}
776+
777+ let rect = Rectangle(Point { x: 0.0, y: 0.0 }, Point { x: 2.0, y: 2.0 });
778+ println!("area: {}", area(rect));
768779~~~~
769780
770781Use a lone ` _ ` to ignore an individual field. Ignore all fields of a variant
@@ -786,8 +797,9 @@ fn point_from_direction(dir: Direction) -> Point {
786797Enum variants may also be structs. For example:
787798
788799~~~~
789- # # ![feature(struct_variant)]
800+ #![feature(struct_variant)]
790801use std::f64;
802+
791803# struct Point { x: f64, y: f64 }
792804# fn square(x: f64) -> f64 { x * x }
793805enum Shape {
@@ -802,7 +814,14 @@ fn area(sh: Shape) -> f64 {
802814 }
803815 }
804816}
805- # fn main() {}
817+
818+ fn main() {
819+ let rect = Rectangle {
820+ top_left: Point { x: 0.0, y: 0.0 },
821+ bottom_right: Point { x: 2.0, y: -2.0 }
822+ };
823+ println!("area: {}", area(rect));
824+ }
806825~~~~
807826
808827> * Note:* This feature of the compiler is currently gated behind the
@@ -986,6 +1005,10 @@ that are `Send`, but non-`Send` types can still *contain* types with custom
9861005destructors. Example of types which are not ` Send ` are [ ` Gc<T> ` ] [ gc ] and
9871006[ ` Rc<T> ` ] [ rc ] , the shared-ownership types.
9881007
1008+ > * Note:* See a [ later chapter] ( #ownership-escape-hatches ) for a discussion about
1009+ > [ ` Gc<T> ` ] [ gc ] and [ ` Rc<T> ` ] [ rc ] , and the [ chapter about traits] ( #traits ) for
1010+ > a discussion about ` Send ` .
1011+
9891012[ gc ] : http://doc.rust-lang.org/std/gc/struct.Gc.html
9901013[ rc ] : http://doc.rust-lang.org/std/rc/struct.Rc.html
9911014
@@ -1647,6 +1670,13 @@ let string = "foobar";
16471670let view: &str = string.slice(0, 3);
16481671~~~
16491672
1673+ Square brackets denote indexing into a slice or fixed-size vector:
1674+
1675+ ~~~~
1676+ let crayons: [&str, ..3] = ["BananaMania", "Beaver", "Bittersweet"];
1677+ println!("Crayon 2 is '{}'", crayons[2]);
1678+ ~~~~
1679+
16501680Mutable slices also exist, just as there are mutable references. However, there
16511681are no mutable string slices. Strings are a multi-byte encoding (UTF-8) of
16521682Unicode code points, so they cannot be freely mutated without the ability to
@@ -1661,20 +1691,6 @@ view[0] = 5;
16611691let ys: &mut [int] = &mut [1i, 2i, 3i];
16621692~~~
16631693
1664- Square brackets denote indexing into a slice or fixed-size vector:
1665-
1666- ~~~~
1667- # enum Crayon { Almond, AntiqueBrass, Apricot,
1668- # Aquamarine, Asparagus, AtomicTangerine,
1669- # BananaMania, Beaver, Bittersweet };
1670- # fn draw_scene(c: Crayon) { }
1671- let crayons: [Crayon, ..3] = [BananaMania, Beaver, Bittersweet];
1672- match crayons[0] {
1673- Bittersweet => draw_scene(crayons[0]),
1674- _ => ()
1675- }
1676- ~~~~
1677-
16781694A slice or fixed-size vector can be destructured using pattern matching:
16791695
16801696~~~~
@@ -1740,6 +1756,8 @@ via dynamic checks and can fail at runtime.
17401756The ` Rc ` and ` Gc ` types are not sendable, so they cannot be used to share memory between tasks. Safe
17411757immutable and mutable shared memory is provided by the ` sync::arc ` module.
17421758
1759+ > * Note:* See a [ later chapter] ( #traits ) for a discussion about ` Send ` and sendable types.
1760+
17431761# Closures
17441762
17451763Named functions, like those we've seen so far, may not refer to local
@@ -2611,7 +2629,7 @@ let nonsense = mycircle.radius() * mycircle.area();
26112629
26122630## Deriving implementations for traits
26132631
2614- A small number of traits in ` std ` and ` extra ` can have implementations
2632+ A small number of traits in can have implementations
26152633that can be automatically derived. These instances are specified by
26162634placing the ` deriving ` attribute on a data type declaration. For
26172635example, the following will mean that ` Circle ` has an implementation
@@ -2620,6 +2638,7 @@ of type `ABC` can be randomly generated and converted to a string:
26202638
26212639~~~
26222640extern crate rand;
2641+ use std::rand::{task_rng, Rng};
26232642
26242643#[deriving(PartialEq)]
26252644struct Circle { radius: f64 }
@@ -2630,6 +2649,13 @@ enum ABC { A, B, C }
26302649fn main() {
26312650 // Use the Show trait to print "A, B, C."
26322651 println!("{}, {}, {}", A, B, C);
2652+
2653+ let mut rng = task_rng();
2654+
2655+ // Use the Rand trait to generate a random variants.
2656+ for _ in range(0i, 10) {
2657+ println!("{}", rng.gen::<ABC>());
2658+ }
26332659}
26342660~~~
26352661
@@ -3138,8 +3164,8 @@ In Rust terminology, we need a way to refer to other crates.
31383164For that, Rust offers you the ` extern crate ` declaration:
31393165
31403166~~~
3167+ // `num` ships with Rust.
31413168extern crate num;
3142- // `num` ships with Rust (much like `extra`; more details further down).
31433169
31443170fn main() {
31453171 // The rational number '1/2':
0 commit comments