Skip to content

Commit 6627ac6

Browse files
committed
Copyedit section 3 of tutorial
1 parent 38ccaed commit 6627ac6

File tree

1 file changed

+53
-45
lines changed

1 file changed

+53
-45
lines changed

doc/tutorial.md

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ and mostly have the same precedence as in C; comments are again like C.
184184

185185
The main surface difference to be aware of is that the condition at
186186
the head of control structures like `if` and `while` do not require
187-
paretheses, while their bodies *must* be wrapped in
188-
brackets. Single-statement, bracket-less bodies are not allowed.
187+
parentheses, while their bodies *must* be wrapped in
188+
braces. Single-statement, unbraced bodies are not allowed.
189189

190190
~~~~
191191
# fn recalibrate_universe() -> bool { true }
@@ -200,9 +200,9 @@ fn main() {
200200
}
201201
~~~~
202202

203-
The `let` keyword introduces a local variable. Variables are immutable
204-
by default, so `let mut` can be used to introduce a local variable
205-
that can be reassigned.
203+
The `let` keyword introduces a local variable. Variables are immutable by
204+
default. To introduce a local variable that you can re-assign later, use `let
205+
mut` instead.
206206

207207
~~~~
208208
let hi = "hi";
@@ -224,14 +224,14 @@ let imaginary_size = monster_size * 10.0;
224224
let monster_size: int = 50;
225225
~~~~
226226

227-
Local variables may shadow earlier declarations, as in the previous
228-
example in which `monster_size` is first declared as a `float`
229-
then a second `monster_size` is declared as an int. If you were to actually
230-
compile this example though, the compiler will see that the second
231-
`monster_size` is unused, assume that you have made a mistake, and issue
232-
a warning. For occasions where unused variables are intentional, their
233-
name may be prefixed with an underscore to silence the warning, like
234-
`let _monster_size = 50;`.
227+
Local variables may shadow earlier declarations, as in the previous example:
228+
`monster_size` was first declared as a `float`, and then then a second
229+
`monster_size` was declared as an int. If you were to actually compile this
230+
example, though, the compiler will determine that the second `monster_size` is
231+
unused and issue a warning (because this situation is likely to indicate a
232+
programmer error). For occasions where unused variables are intentional, their
233+
name may be prefixed with an underscore to silence the warning, like `let
234+
_monster_size = 50;`.
235235

236236
Rust identifiers follow the same rules as C; they start with an alphabetic
237237
character or an underscore, and after that may contain any sequence of
@@ -278,10 +278,10 @@ let price =
278278
};
279279
~~~~
280280

281-
Both pieces of code are exactly equivalentthey assign a value to
281+
Both pieces of code are exactly equivalent: they assign a value to
282282
`price` depending on the condition that holds. Note that there
283-
are not semicolons in the blocks of the second snippet. This is
284-
important; the lack of a semicolon after the last statement in a
283+
are no semicolons in the blocks of the second snippet. This is
284+
important: the lack of a semicolon after the last statement in a
285285
braced block gives the whole block the value of that last expression.
286286

287287
Put another way, the semicolon in Rust *ignores the value of an expression*.
@@ -290,8 +290,10 @@ would simply assign `()` (nil or void) to `price`. But without the semicolon, ea
290290
branch has a different value, and `price` gets the value of the branch that
291291
was taken.
292292

293-
In short, everything that's not a declaration (`let` for variables,
294-
`fn` for functions, et cetera) is an expression, including function bodies.
293+
In short, everything that's not a declaration (declarations are `let` for
294+
variables, `fn` for functions, and any top-level named items such as
295+
[traits](#traits), [enum types](#enums), and [constants](#constants)) is an
296+
expression, including function bodies.
295297

296298
~~~~
297299
fn is_four(x: int) -> bool {
@@ -314,7 +316,7 @@ something—in which case you'll have embedded it in a bigger statement.
314316
# fn foo() -> bool { true }
315317
# fn bar() -> bool { true }
316318
# fn baz() -> bool { true }
317-
// `let` is not an expression, so it is semi-colon terminated;
319+
// `let` is not an expression, so it is semicolon-terminated;
318320
let x = foo();
319321
320322
// When used in statement position, bracy expressions do not
@@ -346,7 +348,7 @@ This may sound intricate, but it is super-useful and will grow on you.
346348
The basic types include the usual boolean, integral, and floating-point types.
347349

348350
------------------------- -----------------------------------------------
349-
`()` Nil, the type that has only a single value
351+
`()` Unit, the type that has only a single value
350352
`bool` Boolean type, with values `true` and `false`
351353
`int`, `uint` Machine-pointer-sized signed and unsigned integers
352354
`i8`, `i16`, `i32`, `i64` Signed integers with a specific size (in bits)
@@ -394,10 +396,15 @@ type MonsterSize = uint;
394396
This will provide a synonym, `MonsterSize`, for unsigned integers. It will not
395397
actually create a new, incompatible type—`MonsterSize` and `uint` can be used
396398
interchangeably, and using one where the other is expected is not a type
397-
error.
399+
error. In that sense, types declared with `type` are *structural*: their
400+
meaning follows from their structure, and their names are irrelevant in the
401+
type system.
398402

399-
To create data types which are not synonyms, `struct` and `enum`
400-
can be used. They're described in more detail below, but they look like this:
403+
Sometimes, you want your data types to be *nominal* instead of structural: you
404+
want their name to be part of their meaning, so that types with the same
405+
structure but different names are not interchangeable. Rust has two ways to
406+
create nominal data types: `struct` and `enum`. They're described in more
407+
detail below, but they look like this:
401408

402409
~~~~
403410
enum HidingPlaces {
@@ -416,12 +423,12 @@ enum MonsterSize = uint; // a single-variant enum
416423

417424
## Literals
418425

419-
Integers can be written in decimal (`144`), hexadecimal (`0x90`), and
426+
Integers can be written in decimal (`144`), hexadecimal (`0x90`), or
420427
binary (`0b10010000`) base. Each integral type has a corresponding literal
421428
suffix that can be used to indicate the type of a literal: `i` for `int`,
422429
`u` for `uint`, and `i8` for the `i8` type, etc.
423430

424-
In the absense of an integer literal suffix, Rust will infer the
431+
In the absence of an integer literal suffix, Rust will infer the
425432
integer type based on type annotations and function signatures in the
426433
surrounding program. In the absence of any type information at all,
427434
Rust will assume that an unsuffixed integer literal has type
@@ -439,24 +446,24 @@ a suffix, the literal is assumed to be of type `float`. Suffixes `f32`
439446
(32-bit) and `f64` (64-bit) can be used to create literals of a
440447
specific type.
441448

442-
The nil literal is written just like the type: `()`. The keywords
449+
The unit literal is written just like the type: `()`. The keywords
443450
`true` and `false` produce the boolean literals.
444451

445-
Character literals are written between single quotes, as in `'x'`. Just as in
452+
Character literals are written between single quotes, as in `'x'`. Just like
446453
C, Rust understands a number of character escapes, using the backslash
447454
character, such as `\n`, `\r`, and `\t`. String literals,
448455
written between double quotes, allow the same escape sequences. Rust strings
449456
may contain newlines.
450457

451458
## Constants
452459

453-
Compile-time constants are declared with `const`. All scalar types,
454-
like integers and floats, may be declared `const`, as well as fixed
455-
length vectors, static strings (more on this later), and structs.
456-
Constants may be declared in any scope and may refer to other
457-
constants. Constant declarations are not type inferred, so must always
458-
have a type annotation. By convention they are written in all capital
459-
letters.
460+
Compile-time constants are declared with `const`. A constant may have any
461+
scalar type (for example, integer or float). Other allowable constant types
462+
are fixed-length vectors, static strings (more on this later), and
463+
structs. Constants may be declared in any scope and may refer to other
464+
constants. The compiler does not infer types for constants, so constants must
465+
always be declared with a type annotation. By convention, they are written in
466+
all capital letters.
460467

461468
~~~
462469
// Scalars can be constants
@@ -480,7 +487,7 @@ const MY_STRUCTY_PASSWORD: Password = Password { value: MY_PASSWORD };
480487

481488
Rust's set of operators contains very few surprises. Arithmetic is done with
482489
`*`, `/`, `%`, `+`, and `-` (multiply, divide, remainder, plus, minus). `-` is
483-
also a unary prefix operator that does negation. As in C, the bit operators
490+
also a unary prefix operator that negates numbers. As in C, the bit operators
484491
`>>`, `<<`, `&`, `|`, and `^` are also supported.
485492

486493
Note that, if applied to an integer value, `!` flips all the bits (like `~` in
@@ -502,21 +509,21 @@ assert y == 4u;
502509
~~~~
503510

504511
The main difference with C is that `++` and `--` are missing, and that
505-
the logical bitwise operators have higher precedencein C, `x & 2 > 0`
512+
the logical bitwise operators have higher precedencein C, `x & 2 > 0`
506513
means `x & (2 > 0)`, but in Rust, it means `(x & 2) > 0`, which is
507-
more likely what a novice expects.
514+
more likely to be what a novice expects.
508515

509516
## Syntax extensions
510517

511518
*Syntax extensions* are special forms that are not built into the language,
512519
but are instead provided by the libraries. To make it clear to the reader when
513-
a syntax extension is being used, the names of all syntax extensions end with
514-
`!`. The standard library defines a few syntax extensions, the most useful of
515-
which is `fmt!`, a `sprintf`-style text formatter that is expanded at compile
516-
time.
520+
a name refers to a syntax extension, the names of all syntax extensions end
521+
with `!`. The standard library defines a few syntax extensions, the most
522+
useful of which is `fmt!`, a `sprintf`-style text formatter that an early
523+
compiler phase expands statically.
517524

518-
`fmt!` supports most of the directives that [printf][pf] supports, but
519-
will give you a compile-time error when the types of the directives
525+
`fmt!` supports most of the directives that [printf][pf] supports, but unlike
526+
printf, will give you a compile-time error when the types of the directives
520527
don't match the types of the arguments.
521528

522529
~~~~
@@ -530,8 +537,9 @@ io::println(fmt!("what is this thing: %?", mystery_object));
530537

531538
[pf]: http://en.cppreference.com/w/cpp/io/c/fprintf
532539

533-
You can define your own syntax extensions with the macro system, which is out
534-
of scope of this tutorial.
540+
You can define your own syntax extensions with the macro system. For details, see the [macro tutorial][macros].
541+
542+
[macros]: tutorial-macros.html
535543

536544
# Control structures
537545

0 commit comments

Comments
 (0)