@@ -702,11 +702,11 @@ collection, Rust uses [higher-order functions](#closures).
702
702
Rust struct types must be declared before they are used using the ` struct `
703
703
syntax: ` struct Name { field1: T1, field2: T2 [, ...] } ` , where ` T1 ` , ` T2 ` ,
704
704
... denote types. To construct a struct, use the same syntax, but leave off
705
- the ` struct ` ; for example: ` Point { x: 1.0, y: 2.0 } ` .
705
+ the ` struct ` : for example: ` Point { x: 1.0, y: 2.0 } ` .
706
706
707
707
Structs are quite similar to C structs and are even laid out the same way in
708
- memory (so you can read from a Rust struct in C, and vice-versa). The dot
709
- operator is used to access struct fields ( ` mypoint.x ` ) .
708
+ memory (so you can read from a Rust struct in C, and vice-versa). Use the dot
709
+ operator to access struct fields, as in ` mypoint.x ` .
710
710
711
711
Fields that you want to mutate must be explicitly marked ` mut ` .
712
712
@@ -720,7 +720,7 @@ struct Stack {
720
720
With a value of such a type, you can do ` mystack.head += 1 ` . If ` mut ` were
721
721
omitted from the type, such an assignment would result in a type error.
722
722
723
- Structs can be destructured in ` match ` patterns. The basic syntax is
723
+ ` match ` patterns destructure structs . The basic syntax is
724
724
` Name {fieldname: pattern, ...} ` :
725
725
726
726
~~~~
@@ -747,9 +747,9 @@ match mypoint {
747
747
}
748
748
~~~
749
749
750
- Structs are the only type in Rust that may have user-defined destructors,
751
- using ` drop ` blocks, inside of which the struct's value may be referred
752
- to with the name ` self ` .
750
+ Structs are the only type in Rust that may have user-defined
751
+ destructors, defined with ` drop ` blocks. Inside a ` drop ` , the name
752
+ ` self ` refers to the struct's value .
753
753
754
754
~~~
755
755
struct TimeBomb {
@@ -783,16 +783,16 @@ A value of this type is either a `Circle`, in which case it contains a
783
783
` Point ` struct and a float, or a ` Rectangle ` , in which case it contains
784
784
two ` Point ` structs. The run-time representation of such a value
785
785
includes an identifier of the actual form that it holds, much like the
786
- 'tagged union' pattern in C, but with better ergonomics .
786
+ 'tagged union' pattern in C, but with better static guarantees .
787
787
788
- The above declaration will define a type ` Shape ` that can be used to
789
- refer to such shapes, and two functions, ` Circle ` and ` Rectangle ` ,
790
- which can be used to construct values of the type (taking arguments of
791
- the specified types). So ` Circle(Point {x: 0f, y: 0f}, 10f) ` is the way to
788
+ The above declaration will define a type ` Shape ` that can refer to
789
+ such shapes, and two functions, ` Circle ` and ` Rectangle ` , which can be
790
+ used to construct values of the type (taking arguments of the
791
+ specified types). So ` Circle(Point {x: 0f, y: 0f}, 10f) ` is the way to
792
792
create a new circle.
793
793
794
- Enum variants need not have type parameters. This, for example, is
795
- equivalent to a C enum:
794
+ Enum variants need not have type parameters. This ` enum ` declaration,
795
+ for example, is equivalent to a C enum:
796
796
797
797
~~~~
798
798
enum Direction {
@@ -803,12 +803,12 @@ enum Direction {
803
803
}
804
804
~~~~
805
805
806
- This will define ` North ` , ` East ` , ` South ` , and ` West ` as constants,
806
+ This declaration defines ` North ` , ` East ` , ` South ` , and ` West ` as constants,
807
807
all of which have type ` Direction ` .
808
808
809
- When an enum is C-like, that is, when none of the variants have
810
- parameters, it is possible to explicitly set the discriminator values
811
- to an integer value:
809
+ When an enum is C-like ( that is, when none of the variants have
810
+ parameters) , it is possible to explicitly set the discriminator values
811
+ to a constant value:
812
812
813
813
~~~~
814
814
enum Color {
@@ -821,16 +821,19 @@ enum Color {
821
821
If an explicit discriminator is not specified for a variant, the value
822
822
defaults to the value of the previous variant plus one. If the first
823
823
variant does not have a discriminator, it defaults to 0. For example,
824
- the value of ` North ` is 0, ` East ` is 1, etc .
824
+ the value of ` North ` is 0, ` East ` is 1, ` South ` is 2, and ` West ` is 3 .
825
825
826
- When an enum is C-like the ` as ` cast operator can be used to get the
827
- discriminator's value.
826
+ When an enum is C-like, you can apply the ` as ` cast operator to
827
+ convert it to its discriminator value as an int .
828
828
829
829
<a name =" single_variant_enum " ></a >
830
830
831
- There is a special case for enums with a single variant. These are
832
- used to define new types in such a way that the new name is not just a
833
- synonym for an existing type, but its own distinct type. If you say:
831
+ There is a special case for enums with a single variant, which are
832
+ sometimes called "newtype-style enums" (after Haskell's "newtype"
833
+ feature). These are used to define new types in such a way that the
834
+ new name is not just a synonym for an existing type, but its own
835
+ distinct type: ` type ` creates a structural synonym, while this form of
836
+ ` enum ` creates a nominal synonym. If you say:
834
837
835
838
~~~~
836
839
enum GizmoId = int;
@@ -842,7 +845,7 @@ That is a shorthand for this:
842
845
enum GizmoId { GizmoId(int) }
843
846
~~~~
844
847
845
- Enum types like this can have their content extracted with the
848
+ You can extract the contents of such an enum type with the
846
849
dereference (` * ` ) unary operator:
847
850
848
851
~~~~
@@ -851,6 +854,17 @@ let my_gizmo_id: GizmoId = GizmoId(10);
851
854
let id_int: int = *my_gizmo_id;
852
855
~~~~
853
856
857
+ Types like this can be useful to differentiate between data that have
858
+ the same type but must be used in different ways.
859
+
860
+ ~~~~
861
+ enum Inches = int;
862
+ enum Centimeters = int;
863
+ ~~~~
864
+
865
+ The above definitions allow for a simple way for programs to avoid
866
+ confusing numbers that correspond to different units.
867
+
854
868
For enum types with multiple variants, destructuring is the only way to
855
869
get at their contents. All variant constructors can be used as
856
870
patterns, as in this definition of ` area ` :
@@ -866,9 +880,9 @@ fn area(sh: Shape) -> float {
866
880
}
867
881
~~~~
868
882
869
- Like other patterns, a lone underscore ignores individual fields.
870
- Ignoring all fields of a variant can be written ` Circle(*) ` . As in
871
- their introductory form, nullary enum patterns are written without
883
+ You can write a lone ` _ ` to ignore an individual fields, and can
884
+ ignore all fields of a variant like: ` Circle(*) ` . As in their
885
+ introduction form, nullary enum patterns are written without
872
886
parentheses.
873
887
874
888
~~~~
@@ -887,9 +901,9 @@ fn point_from_direction(dir: Direction) -> Point {
887
901
## Tuples
888
902
889
903
Tuples in Rust behave exactly like structs, except that their fields
890
- do not have names (and can thus not be accessed with dot notation) .
904
+ do not have names. Thus, you cannot access their fields with dot notation.
891
905
Tuples can have any arity except for 0 or 1 (though you may consider
892
- nil , ` () ` , as the empty tuple if you like).
906
+ unit , ` () ` , as the empty tuple if you like).
893
907
894
908
~~~~
895
909
let mytup: (int, int, float) = (10, 20, 30.0);
@@ -902,10 +916,11 @@ match mytup {
902
916
903
917
We've already seen several function definitions. Like all other static
904
918
declarations, such as ` type ` , functions can be declared both at the
905
- top level and inside other functions (or modules, which we'll come
906
- back to [ later] ( #modules-and-crates ) ). They are introduced with the
907
- ` fn ` keyword, the type of arguments are specified following colons and
908
- the return type follows the arrow.
919
+ top level and inside other functions (or in modules, which we'll come
920
+ back to [ later] ( #modules-and-crates ) ). The ` fn ` keyword introduces a
921
+ function. A function has an argument list, which is a parenthesized
922
+ list of ` expr: type ` pairs separated by commas. An arrow ` -> `
923
+ separates the argument list and the function's return type.
909
924
910
925
~~~~
911
926
fn line(a: int, b: int, x: int) -> int {
@@ -924,9 +939,12 @@ fn line(a: int, b: int, x: int) -> int {
924
939
}
925
940
~~~~
926
941
927
- Functions that do not return a value are said to return nil, ` () ` ,
928
- and both the return type and the return value may be omitted from
929
- the definition. The following two functions are equivalent.
942
+ It's better Rust style to write a return value this way instead of
943
+ writing an explicit ` return ` . The utility of ` return ` comes in when
944
+ returning early from a function. Functions that do not return a value
945
+ are said to return nil, ` () ` , and both the return type and the return
946
+ value may be omitted from the definition. The following two functions
947
+ are equivalent.
930
948
931
949
~~~~
932
950
fn do_nothing_the_hard_way() -> () { return (); }
@@ -944,10 +962,12 @@ assert 8 == line(5, 3, 1);
944
962
assert () == oops(5, 3, 1);
945
963
~~~~
946
964
947
- Methods are like functions, except that they are defined for a specific
948
- 'self' type (like 'this' in C++). Calling a method is done with
949
- dot notation, as in ` my_vec.len() ` . Methods may be defined on most
950
- Rust types with the ` impl ` keyword. As an example, lets define a draw
965
+ Methods are like functions, except that they have an implicit argument
966
+ called ` self ` , which has the type that the method's receiver has. The
967
+ ` self ` argument is like 'this' in C++. An expression with dot
968
+ notation, as in ` my_vec.len() ` , denotes a method
969
+ call. Implementations, written with the ` impl ` keyword, can define
970
+ methods on most Rust types. As an example, let's define a ` draw `
951
971
method on our ` Shape ` enum.
952
972
953
973
~~~
@@ -978,15 +998,15 @@ s.draw();
978
998
979
999
This defines an _ implementation_ for ` Shape ` containing a single
980
1000
method, ` draw ` . In most respects the ` draw ` method is defined
981
- like any other function, with the exception of the name ` self ` . ` self `
982
- is a special value that is automatically defined in each method,
1001
+ like any other function, except for the name ` self ` . ` self `
1002
+ is a special value that is automatically in scope inside each method,
983
1003
referring to the value being operated on. If we wanted we could add
984
1004
additional methods to the same impl, or multiple impls for the same
985
1005
type. We'll discuss methods more in the context of [ traits and
986
1006
generics] ( #generics ) .
987
1007
988
- > *** Note:*** The method definition syntax will change to require
989
- > declaring the self type explicitly, as the first argument.
1008
+ > *** Note:*** In the future, the method definition syntax will change to
1009
+ > require declaring the ` self ` type explicitly, as the first argument.
990
1010
991
1011
# The Rust memory model
992
1012
0 commit comments