@@ -716,8 +716,8 @@ When an enum has simple integer discriminators, you can apply the `as` cast
716
716
operator to convert a variant to its discriminator value as an ` int ` :
717
717
718
718
~~~~
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 );
721
721
~~~~
722
722
723
723
It 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
748
748
749
749
This declaration defines a type ` Shape ` that can refer to such shapes, and two
750
750
functions, ` Circle ` and ` Rectangle ` , which can be used to construct values of
751
- the type. To create a new Circle, write `Circle(Point { x: 0.0, y: 0.0 },
752
- 10.0)`.
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
+ ~~~~
753
760
754
761
All of these variant constructors may be used as patterns. The only way to
755
762
access the contents of an enum instance is the destructuring of a match. For
756
763
example:
757
764
758
765
~~~~
759
766
use std::f64;
767
+
760
768
# struct Point {x: f64, y: f64}
761
769
# enum Shape { Circle(Point, f64), Rectangle(Point, Point) }
762
770
fn area(sh: Shape) -> f64 {
@@ -765,6 +773,9 @@ fn area(sh: Shape) -> f64 {
765
773
Rectangle(Point { x, y }, Point { x: x2, y: y2 }) => (x2 - x) * (y2 - y)
766
774
}
767
775
}
776
+
777
+ let rect = Rectangle(Point { x: 0.0, y: 0.0 }, Point { x: 2.0, y: 2.0 });
778
+ println!("area: {}", area(rect));
768
779
~~~~
769
780
770
781
Use a lone ` _ ` to ignore an individual field. Ignore all fields of a variant
@@ -786,8 +797,9 @@ fn point_from_direction(dir: Direction) -> Point {
786
797
Enum variants may also be structs. For example:
787
798
788
799
~~~~
789
- # # ![feature(struct_variant)]
800
+ #![feature(struct_variant)]
790
801
use std::f64;
802
+
791
803
# struct Point { x: f64, y: f64 }
792
804
# fn square(x: f64) -> f64 { x * x }
793
805
enum Shape {
@@ -802,7 +814,14 @@ fn area(sh: Shape) -> f64 {
802
814
}
803
815
}
804
816
}
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
+ }
806
825
~~~~
807
826
808
827
> * 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
986
1005
destructors. Example of types which are not ` Send ` are [ ` Gc<T> ` ] [ gc ] and
987
1006
[ ` Rc<T> ` ] [ rc ] , the shared-ownership types.
988
1007
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
+
989
1012
[ gc ] : http://doc.rust-lang.org/std/gc/struct.Gc.html
990
1013
[ rc ] : http://doc.rust-lang.org/std/rc/struct.Rc.html
991
1014
@@ -1645,6 +1668,13 @@ let string = "foobar";
1645
1668
let view: &str = string.slice(0, 3);
1646
1669
~~~
1647
1670
1671
+ Square brackets denote indexing into a slice or fixed-size vector:
1672
+
1673
+ ~~~~
1674
+ let crayons: [&str, ..3] = ["BananaMania", "Beaver", "Bittersweet"];
1675
+ println!("Crayon 2 is '{}'", crayons[2]);
1676
+ ~~~~
1677
+
1648
1678
Mutable slices also exist, just as there are mutable references. However, there
1649
1679
are no mutable string slices. Strings are a multi-byte encoding (UTF-8) of
1650
1680
Unicode code points, so they cannot be freely mutated without the ability to
@@ -1659,20 +1689,6 @@ view[0] = 5;
1659
1689
let ys: &mut [int] = &mut [1i, 2i, 3i];
1660
1690
~~~
1661
1691
1662
- Square brackets denote indexing into a slice or fixed-size vector:
1663
-
1664
- ~~~~
1665
- # enum Crayon { Almond, AntiqueBrass, Apricot,
1666
- # Aquamarine, Asparagus, AtomicTangerine,
1667
- # BananaMania, Beaver, Bittersweet };
1668
- # fn draw_scene(c: Crayon) { }
1669
- let crayons: [Crayon, ..3] = [BananaMania, Beaver, Bittersweet];
1670
- match crayons[0] {
1671
- Bittersweet => draw_scene(crayons[0]),
1672
- _ => ()
1673
- }
1674
- ~~~~
1675
-
1676
1692
A slice or fixed-size vector can be destructured using pattern matching:
1677
1693
1678
1694
~~~~
@@ -1738,6 +1754,8 @@ via dynamic checks and can fail at runtime.
1738
1754
The ` Rc ` and ` Gc ` types are not sendable, so they cannot be used to share memory between tasks. Safe
1739
1755
immutable and mutable shared memory is provided by the ` sync::arc ` module.
1740
1756
1757
+ > * Note:* See a [ later chapter] ( #traits ) for a discussion about ` Send ` and sendable types.
1758
+
1741
1759
# Closures
1742
1760
1743
1761
Named functions, like those we've seen so far, may not refer to local
@@ -2609,7 +2627,7 @@ let nonsense = mycircle.radius() * mycircle.area();
2609
2627
2610
2628
## Deriving implementations for traits
2611
2629
2612
- A small number of traits in ` std ` and ` extra ` can have implementations
2630
+ A small number of traits in can have implementations
2613
2631
that can be automatically derived. These instances are specified by
2614
2632
placing the ` deriving ` attribute on a data type declaration. For
2615
2633
example, the following will mean that ` Circle ` has an implementation
@@ -2618,6 +2636,7 @@ of type `ABC` can be randomly generated and converted to a string:
2618
2636
2619
2637
~~~
2620
2638
extern crate rand;
2639
+ use std::rand::{task_rng, Rng};
2621
2640
2622
2641
#[deriving(PartialEq)]
2623
2642
struct Circle { radius: f64 }
@@ -2628,6 +2647,13 @@ enum ABC { A, B, C }
2628
2647
fn main() {
2629
2648
// Use the Show trait to print "A, B, C."
2630
2649
println!("{}, {}, {}", A, B, C);
2650
+
2651
+ let mut rng = task_rng();
2652
+
2653
+ // Use the Rand trait to generate a random variants.
2654
+ for _ in range(0i, 10) {
2655
+ println!("{}", rng.gen::<ABC>());
2656
+ }
2631
2657
}
2632
2658
~~~
2633
2659
@@ -3136,8 +3162,8 @@ In Rust terminology, we need a way to refer to other crates.
3136
3162
For that, Rust offers you the ` extern crate ` declaration:
3137
3163
3138
3164
~~~
3165
+ // `num` ships with Rust.
3139
3166
extern crate num;
3140
- // `num` ships with Rust (much like `extra`; more details further down).
3141
3167
3142
3168
fn main() {
3143
3169
// The rational number '1/2':
0 commit comments