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