@@ -152,9 +152,9 @@ example, by changing `io::println` to some nonexistent function), and
152
152
then compile it, you'll see an error message like this:
153
153
154
154
~~~~ {.notrust}
155
- hello.rs:2:4: 2:16 error: unresolved name: io::print_with_unicorns
156
- hello.rs:2 io::print_with_unicorns ("hello? yes, this is rust");
157
- ^~~~~~~~~~~~~~~~~~~~~~~
155
+ hello.rs:2:4: 2:16 error: unresolved name: io::print_it
156
+ hello.rs:2 io::print_it ("hello? yes, this is rust");
157
+ ^~~~~~~~~~~~
158
158
~~~~
159
159
160
160
In its simplest form, a Rust program is a ` .rs ` file with some types
@@ -178,11 +178,9 @@ included in that directory. In particular, if you are running emacs
178
178
24, then using emacs's internal package manager to install ` rust-mode `
179
179
is the easiest way to keep it up to date. There is also a package for
180
180
Sublime Text 2, available both [ standalone] [ sublime ] and through
181
- [ Sublime Package Control] [ sublime-pkg ] , and support for Kate
182
- under ` src/etc/kate ` .
181
+ [ Sublime Package Control] [ sublime-pkg ] .
183
182
184
- There is ctags support via ` src/etc/ctags.rust ` , but many other
185
- tools and editors are not provided for yet. If you end up writing a Rust
183
+ Other editors are not provided for yet. If you end up writing a Rust
186
184
mode for your favorite editor, let us know so that we can link to it.
187
185
188
186
[ sublime ] : http://github.com/dbp/sublime-rust
@@ -193,7 +191,7 @@ mode for your favorite editor, let us know so that we can link to it.
193
191
Assuming you've programmed in any C-family language (C++, Java,
194
192
JavaScript, C#, or PHP), Rust will feel familiar. Code is arranged
195
193
in blocks delineated by curly braces; there are control structures
196
- for branching and looping, like the familiar ` if ` and ` while ` ; function
194
+ for branching and looping, like the familiar ` if ` and ` when ` ; function
197
195
calls are written ` myfunc(arg1, arg2) ` ; operators are written the same
198
196
and mostly have the same precedence as in C; comments are again like C.
199
197
@@ -229,14 +227,13 @@ while count < 10 {
229
227
}
230
228
~~~~
231
229
232
- Although Rust can almost always infer the types of local variables, you
233
- can specify a variable's type by following it with a colon, then the type
234
- name.
230
+ Although Rust can almost always infer the types of local variables, it
231
+ can help readability to specify a variable's type by following it with
232
+ a colon, then the type name.
235
233
236
234
~~~~
237
- let monster_size: float = 57.8;
238
- let imaginary_size = monster_size * 10;
239
- let monster_size: int = 50;
235
+ let my_favorite_value: float = 57.8;
236
+ let my_favorite_value: int = my_favorite_value as int;
240
237
~~~~
241
238
242
239
Local variables may shadow earlier declarations, as in the previous
@@ -251,14 +248,14 @@ underscores where they help readability, while writing types in camel case.
251
248
252
249
~~~
253
250
let my_variable = 100;
254
- type MyType = int; // some built-in types are _not_ camel case
251
+ type MyType = int; // built-in types though are _not_ camel case
255
252
~~~
256
253
257
254
## Expression syntax
258
255
259
256
Though it isn't apparent in all code, there is a fundamental
260
- difference between Rust's syntax and predecessors like C.
261
- Many constructs that are statements in C are expressions
257
+ difference between Rust's syntax and its predecessors in this family
258
+ of languages. Many constructs that are statements in C are expressions
262
259
in Rust, allowing code to be more concise. For example, you might
263
260
write a piece of code like this:
264
261
@@ -278,25 +275,24 @@ But, in Rust, you don't have to repeat the name `price`:
278
275
279
276
~~~~
280
277
# let item = "salad";
281
- let price =
282
- if item == "salad" {
283
- 3.50
284
- } else if item == "muffin" {
285
- 2.25
286
- } else {
287
- 2.00
288
- };
278
+ let price = if item == "salad" {
279
+ 3.50
280
+ } else if item == "muffin" {
281
+ 2.25
282
+ } else {
283
+ 2.00
284
+ };
289
285
~~~~
290
286
291
287
Both pieces of code are exactly equivalent—they assign a value to
292
- ` price ` depending on the condition that holds. Note that there
293
- are not semicolons in the blocks of the second snippet. This is
288
+ ` price ` depending on the condition that holds. Note that the
289
+ semicolons are omitted from the blocks in the second snippet. This is
294
290
important; the lack of a semicolon after the last statement in a
295
291
braced block gives the whole block the value of that last expression.
296
292
297
293
Put another way, the semicolon in Rust * ignores the value of an expression* .
298
294
Thus, if the branches of the ` if ` had looked like ` { 4; } ` , the above example
299
- would simply assign ` () ` ( nil or void) to ` price ` . But without the semicolon, each
295
+ would simply assign nil ( void) to ` price ` . But without the semicolon, each
300
296
branch has a different value, and ` price ` gets the value of the branch that
301
297
was taken.
302
298
@@ -350,7 +346,8 @@ if x {
350
346
let y = if x { foo() } else { bar() };
351
347
~~~
352
348
353
- This may sound a intricate, but it is super-useful and will grow on you.
349
+ This may sound a bit intricate, but it is super-useful, and it will
350
+ grow on you (hopefully).
354
351
355
352
## Types
356
353
@@ -368,8 +365,7 @@ The basic types include the usual boolean, integral, and floating point types.
368
365
------------------------- -----------------------------------------------
369
366
370
367
These can be combined in composite types, which will be described in
371
- more detail later on (the ` T ` s here stand for any other type,
372
- while N should be a literal number):
368
+ more detail later on (the ` T ` s here stand for any other type):
373
369
374
370
------------------------- -----------------------------------------------
375
371
` [T * N] ` Vector (like an array in other languages) with N elements
@@ -396,7 +392,7 @@ the type `fn() -> bool` or the function declaration `fn foo() -> bool
396
392
optionally write ` -> () ` , but usually the return annotation is simply
397
393
left off, as in ` fn main() { ... } ` .
398
394
399
- Types can be given names or aliases with ` type ` declarations:
395
+ Types can be given names with ` type ` declarations:
400
396
401
397
~~~~
402
398
type MonsterSize = uint;
@@ -405,25 +401,9 @@ type MonsterSize = uint;
405
401
This will provide a synonym, ` MonsterSize ` , for unsigned integers. It will not
406
402
actually create a new, incompatible type—` MonsterSize ` and ` uint ` can be used
407
403
interchangeably, and using one where the other is expected is not a type
408
- error.
409
-
410
- To create data types which are not synonyms, ` struct ` and ` enum `
411
- can be used. They're described in more detail below, but they look like this:
412
-
413
- ~~~~
414
- enum HidingPlaces {
415
- Closet(uint),
416
- UnderTheBed(uint)
417
- }
418
-
419
- struct HeroicBabysitter {
420
- bedtime_stories: uint,
421
- sharpened_stakes: uint
422
- }
423
-
424
- struct BabysitterSize(uint); // a single-variant struct
425
- enum MonsterSize = uint; // a single-variant enum
426
- ~~~~
404
+ error. Read about [ single-variant enums] ( #single_variant_enum )
405
+ further on if you need to create a type name that's not just a
406
+ synonym.
427
407
428
408
## Literals
429
409
@@ -455,7 +435,7 @@ The nil literal is written just like the type: `()`. The keywords
455
435
456
436
Character literals are written between single quotes, as in ` 'x' ` . Just as in
457
437
C, Rust understands a number of character escapes, using the backslash
458
- character, such as ` \n ` , ` \r ` , and ` \t ` . String literals,
438
+ character, ` \n ` , ` \r ` , and ` \t ` being the most common . String literals,
459
439
written between double quotes, allow the same escape sequences. Rust strings
460
440
may contain newlines.
461
441
@@ -486,8 +466,8 @@ assert y == 4u;
486
466
487
467
The main difference with C is that ` ++ ` and ` -- ` are missing, and that
488
468
the logical bitwise operators have higher precedence — in C, ` x & 2 > 0 `
489
- means ` x & (2 > 0) ` , but in Rust, it means ` (x & 2) > 0 ` , which is
490
- more likely what a novice expects .
469
+ comes out as ` x & (2 > 0) ` , in Rust, it means ` (x & 2) > 0 ` , which is
470
+ more likely to be what you expect (unless you are a C veteran) .
491
471
492
472
## Syntax extensions
493
473
@@ -505,7 +485,7 @@ don't match the types of the arguments.
505
485
~~~~
506
486
# let mystery_object = ();
507
487
508
- io::println(fmt!("%s is %d", "the answer", 42 ));
488
+ io::println(fmt!("%s is %d", "the answer", 43 ));
509
489
510
490
// %? will conveniently print any type
511
491
io::println(fmt!("what is this thing: %?", mystery_object));
@@ -576,14 +556,18 @@ underscore (`_`) is a wildcard pattern that matches everything.
576
556
577
557
The patterns in an match arm are followed by a fat arrow, ` => ` , then an
578
558
expression to evaluate. Each case is separated by commas. It's often
579
- convenient to use a block expression for each case, in which case the
559
+ convenient to use a block expression for a case, in which case the
580
560
commas are optional.
581
561
582
562
~~~
583
563
# let my_number = 1;
584
564
match my_number {
585
- 0 => { io::println("zero") }
586
- _ => { io::println("something else") }
565
+ 0 => {
566
+ io::println("zero")
567
+ }
568
+ _ => {
569
+ io::println("something else")
570
+ }
587
571
}
588
572
~~~
589
573
0 commit comments