@@ -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
@@ -1647,6 +1670,13 @@ let string = "foobar";
1647
1670
let view: &str = string.slice(0, 3);
1648
1671
~~~
1649
1672
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
+
1650
1680
Mutable slices also exist, just as there are mutable references. However, there
1651
1681
are no mutable string slices. Strings are a multi-byte encoding (UTF-8) of
1652
1682
Unicode code points, so they cannot be freely mutated without the ability to
@@ -1661,20 +1691,6 @@ view[0] = 5;
1661
1691
let ys: &mut [int] = &mut [1i, 2i, 3i];
1662
1692
~~~
1663
1693
1664
- Square brackets denote indexing into a slice or fixed-size vector:
1665
-
1666
- ~~~~
1667
- # enum Crayon { Almond, AntiqueBrass, Apricot,
1668
- # Aquamarine, Asparagus, AtomicTangerine,
1669
- # BananaMania, Beaver, Bittersweet };
1670
- # fn draw_scene(c: Crayon) { }
1671
- let crayons: [Crayon, ..3] = [BananaMania, Beaver, Bittersweet];
1672
- match crayons[0] {
1673
- Bittersweet => draw_scene(crayons[0]),
1674
- _ => ()
1675
- }
1676
- ~~~~
1677
-
1678
1694
A slice or fixed-size vector can be destructured using pattern matching:
1679
1695
1680
1696
~~~~
@@ -1740,6 +1756,8 @@ via dynamic checks and can fail at runtime.
1740
1756
The ` Rc ` and ` Gc ` types are not sendable, so they cannot be used to share memory between tasks. Safe
1741
1757
immutable and mutable shared memory is provided by the ` sync::arc ` module.
1742
1758
1759
+ > * Note:* See a [ later chapter] ( #traits ) for a discussion about ` Send ` and sendable types.
1760
+
1743
1761
# Closures
1744
1762
1745
1763
Named functions, like those we've seen so far, may not refer to local
@@ -2611,7 +2629,7 @@ let nonsense = mycircle.radius() * mycircle.area();
2611
2629
2612
2630
## Deriving implementations for traits
2613
2631
2614
- A small number of traits in ` std ` and ` extra ` can have implementations
2632
+ A small number of traits in can have implementations
2615
2633
that can be automatically derived. These instances are specified by
2616
2634
placing the ` deriving ` attribute on a data type declaration. For
2617
2635
example, 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:
2620
2638
2621
2639
~~~
2622
2640
extern crate rand;
2641
+ use std::rand::{task_rng, Rng};
2623
2642
2624
2643
#[deriving(PartialEq)]
2625
2644
struct Circle { radius: f64 }
@@ -2630,6 +2649,13 @@ enum ABC { A, B, C }
2630
2649
fn main() {
2631
2650
// Use the Show trait to print "A, B, C."
2632
2651
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
+ }
2633
2659
}
2634
2660
~~~
2635
2661
@@ -3138,8 +3164,8 @@ In Rust terminology, we need a way to refer to other crates.
3138
3164
For that, Rust offers you the ` extern crate ` declaration:
3139
3165
3140
3166
~~~
3167
+ // `num` ships with Rust.
3141
3168
extern crate num;
3142
- // `num` ships with Rust (much like `extra`; more details further down).
3143
3169
3144
3170
fn main() {
3145
3171
// The rational number '1/2':
0 commit comments