Skip to content

Commit 1a8b00a

Browse files
committed
Copyedit sections 5 and 6 of the tutorial
1 parent d7b8512 commit 1a8b00a

File tree

1 file changed

+65
-45
lines changed

1 file changed

+65
-45
lines changed

doc/tutorial.md

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -702,11 +702,11 @@ collection, Rust uses [higher-order functions](#closures).
702702
Rust struct types must be declared before they are used using the `struct`
703703
syntax: `struct Name { field1: T1, field2: T2 [, ...] }`, where `T1`, `T2`,
704704
... 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 }`.
706706

707707
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`.
710710

711711
Fields that you want to mutate must be explicitly marked `mut`.
712712

@@ -720,7 +720,7 @@ struct Stack {
720720
With a value of such a type, you can do `mystack.head += 1`. If `mut` were
721721
omitted from the type, such an assignment would result in a type error.
722722

723-
Structs can be destructured in `match` patterns. The basic syntax is
723+
`match` patterns destructure structs. The basic syntax is
724724
`Name {fieldname: pattern, ...}`:
725725

726726
~~~~
@@ -747,9 +747,9 @@ match mypoint {
747747
}
748748
~~~
749749

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.
753753

754754
~~~
755755
struct TimeBomb {
@@ -783,16 +783,16 @@ A value of this type is either a `Circle`, in which case it contains a
783783
`Point` struct and a float, or a `Rectangle`, in which case it contains
784784
two `Point` structs. The run-time representation of such a value
785785
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.
787787

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
792792
create a new circle.
793793

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:
796796

797797
~~~~
798798
enum Direction {
@@ -803,12 +803,12 @@ enum Direction {
803803
}
804804
~~~~
805805

806-
This will define `North`, `East`, `South`, and `West` as constants,
806+
This declaration defines `North`, `East`, `South`, and `West` as constants,
807807
all of which have type `Direction`.
808808

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:
812812

813813
~~~~
814814
enum Color {
@@ -821,16 +821,19 @@ enum Color {
821821
If an explicit discriminator is not specified for a variant, the value
822822
defaults to the value of the previous variant plus one. If the first
823823
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.
825825

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.
828828

829829
<a name="single_variant_enum"></a>
830830

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:
834837

835838
~~~~
836839
enum GizmoId = int;
@@ -842,7 +845,7 @@ That is a shorthand for this:
842845
enum GizmoId { GizmoId(int) }
843846
~~~~
844847

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
846849
dereference (`*`) unary operator:
847850

848851
~~~~
@@ -851,6 +854,17 @@ let my_gizmo_id: GizmoId = GizmoId(10);
851854
let id_int: int = *my_gizmo_id;
852855
~~~~
853856

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+
854868
For enum types with multiple variants, destructuring is the only way to
855869
get at their contents. All variant constructors can be used as
856870
patterns, as in this definition of `area`:
@@ -866,9 +880,9 @@ fn area(sh: Shape) -> float {
866880
}
867881
~~~~
868882

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
872886
parentheses.
873887

874888
~~~~
@@ -887,9 +901,9 @@ fn point_from_direction(dir: Direction) -> Point {
887901
## Tuples
888902

889903
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.
891905
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).
893907

894908
~~~~
895909
let mytup: (int, int, float) = (10, 20, 30.0);
@@ -902,10 +916,11 @@ match mytup {
902916

903917
We've already seen several function definitions. Like all other static
904918
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.
909924

910925
~~~~
911926
fn line(a: int, b: int, x: int) -> int {
@@ -924,9 +939,12 @@ fn line(a: int, b: int, x: int) -> int {
924939
}
925940
~~~~
926941

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.
930948

931949
~~~~
932950
fn do_nothing_the_hard_way() -> () { return (); }
@@ -944,10 +962,12 @@ assert 8 == line(5, 3, 1);
944962
assert () == oops(5, 3, 1);
945963
~~~~
946964

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`
951971
method on our `Shape` enum.
952972

953973
~~~
@@ -978,15 +998,15 @@ s.draw();
978998

979999
This defines an _implementation_ for `Shape` containing a single
9801000
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,
9831003
referring to the value being operated on. If we wanted we could add
9841004
additional methods to the same impl, or multiple impls for the same
9851005
type. We'll discuss methods more in the context of [traits and
9861006
generics](#generics).
9871007

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.
9901010
9911011
# The Rust memory model
9921012

0 commit comments

Comments
 (0)