@@ -184,8 +184,8 @@ and mostly have the same precedence as in C; comments are again like C.
184
184
185
185
The main surface difference to be aware of is that the condition at
186
186
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.
189
189
190
190
~~~~
191
191
# fn recalibrate_universe() -> bool { true }
@@ -200,9 +200,9 @@ fn main() {
200
200
}
201
201
~~~~
202
202
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 .
206
206
207
207
~~~~
208
208
let hi = "hi";
@@ -224,14 +224,14 @@ let imaginary_size = monster_size * 10.0;
224
224
let monster_size: int = 50;
225
225
~~~~
226
226
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;`.
235
235
236
236
Rust identifiers follow the same rules as C; they start with an alphabetic
237
237
character or an underscore, and after that may contain any sequence of
@@ -278,10 +278,10 @@ let price =
278
278
};
279
279
~~~~
280
280
281
- Both pieces of code are exactly equivalent— they assign a value to
281
+ Both pieces of code are exactly equivalent: they assign a value to
282
282
` 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
285
285
braced block gives the whole block the value of that last expression.
286
286
287
287
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
290
290
branch has a different value, and ` price ` gets the value of the branch that
291
291
was taken.
292
292
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.
295
297
296
298
~~~~
297
299
fn is_four(x: int) -> bool {
@@ -314,7 +316,7 @@ something—in which case you'll have embedded it in a bigger statement.
314
316
# fn foo() -> bool { true }
315
317
# fn bar() -> bool { true }
316
318
# 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;
318
320
let x = foo();
319
321
320
322
// 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.
346
348
The basic types include the usual boolean, integral, and floating-point types.
347
349
348
350
------------------------- -----------------------------------------------
349
- ` () ` Nil , the type that has only a single value
351
+ ` () ` Unit , the type that has only a single value
350
352
` bool ` Boolean type, with values ` true ` and ` false `
351
353
` int ` , ` uint ` Machine-pointer-sized signed and unsigned integers
352
354
` i8 ` , ` i16 ` , ` i32 ` , ` i64 ` Signed integers with a specific size (in bits)
@@ -394,10 +396,15 @@ type MonsterSize = uint;
394
396
This will provide a synonym, ` MonsterSize ` , for unsigned integers. It will not
395
397
actually create a new, incompatible type—` MonsterSize ` and ` uint ` can be used
396
398
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.
398
402
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:
401
408
402
409
~~~~
403
410
enum HidingPlaces {
@@ -416,12 +423,12 @@ enum MonsterSize = uint; // a single-variant enum
416
423
417
424
## Literals
418
425
419
- Integers can be written in decimal (` 144 ` ), hexadecimal (` 0x90 ` ), and
426
+ Integers can be written in decimal (` 144 ` ), hexadecimal (` 0x90 ` ), or
420
427
binary (` 0b10010000 ` ) base. Each integral type has a corresponding literal
421
428
suffix that can be used to indicate the type of a literal: ` i ` for ` int ` ,
422
429
` u ` for ` uint ` , and ` i8 ` for the ` i8 ` type, etc.
423
430
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
425
432
integer type based on type annotations and function signatures in the
426
433
surrounding program. In the absence of any type information at all,
427
434
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`
439
446
(32-bit) and ` f64 ` (64-bit) can be used to create literals of a
440
447
specific type.
441
448
442
- The nil literal is written just like the type: ` () ` . The keywords
449
+ The unit literal is written just like the type: ` () ` . The keywords
443
450
` true ` and ` false ` produce the boolean literals.
444
451
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
446
453
C, Rust understands a number of character escapes, using the backslash
447
454
character, such as ` \n ` , ` \r ` , and ` \t ` . String literals,
448
455
written between double quotes, allow the same escape sequences. Rust strings
449
456
may contain newlines.
450
457
451
458
## Constants
452
459
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.
460
467
461
468
~~~
462
469
// Scalars can be constants
@@ -480,7 +487,7 @@ const MY_STRUCTY_PASSWORD: Password = Password { value: MY_PASSWORD };
480
487
481
488
Rust's set of operators contains very few surprises. Arithmetic is done with
482
489
` * ` , ` / ` , ` % ` , ` + ` , 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
484
491
` >> ` , ` << ` , ` & ` , ` | ` , and ` ^ ` are also supported.
485
492
486
493
Note that, if applied to an integer value, ` ! ` flips all the bits (like ` ~ ` in
@@ -502,21 +509,21 @@ assert y == 4u;
502
509
~~~~
503
510
504
511
The main difference with C is that ` ++ ` and ` -- ` are missing, and that
505
- the logical bitwise operators have higher precedence — in C, ` x & 2 > 0 `
512
+ the logical bitwise operators have higher precedence— in C, ` x & 2 > 0 `
506
513
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.
508
515
509
516
## Syntax extensions
510
517
511
518
* Syntax extensions* are special forms that are not built into the language,
512
519
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 .
517
524
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
520
527
don't match the types of the arguments.
521
528
522
529
~~~~
@@ -530,8 +537,9 @@ io::println(fmt!("what is this thing: %?", mystery_object));
530
537
531
538
[ pf ] : http://en.cppreference.com/w/cpp/io/c/fprintf
532
539
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
535
543
536
544
# Control structures
537
545
0 commit comments