@@ -418,6 +418,19 @@ let x: int = 5;
418
418
If I asked you to read this out loud to the rest of the class, you'd say "` x `
419
419
is a binding with the type ` int ` and the value ` five ` ."
420
420
421
+ In future examples, we may annotate the type in a comment. The examples will
422
+ look like this:
423
+
424
+ ``` {rust}
425
+ fn main() {
426
+ let x = 5i; // x: int
427
+ }
428
+ ```
429
+
430
+ Note the similarities between this annotation and the syntax you use with ` let ` .
431
+ Including these kinds of comments is not idiomatic Rust, but we'll occasionally
432
+ include them to help you understand what the types that Rust infers are.
433
+
421
434
By default, bindings are ** immutable** . This code will not compile:
422
435
423
436
``` {ignore}
@@ -436,7 +449,7 @@ error: re-assignment of immutable variable `x`
436
449
If you want a binding to be mutable, you can use ` mut ` :
437
450
438
451
``` {rust}
439
- let mut x = 5i;
452
+ let mut x = 5i; // mut x: int
440
453
x = 10i;
441
454
```
442
455
@@ -584,15 +597,15 @@ let y = if x == 5i {
584
597
10i
585
598
} else {
586
599
15i
587
- };
600
+ }; // y: int
588
601
```
589
602
590
603
Which we can (and probably should) write like this:
591
604
592
605
``` {rust}
593
606
let x = 5i;
594
607
595
- let y = if x == 5i { 10i } else { 15i };
608
+ let y = if x == 5i { 10i } else { 15i }; // y: int
596
609
```
597
610
598
611
This reveals two interesting things about Rust: it is an expression-based
@@ -928,8 +941,8 @@ destructuring. You can assign one tuple into another, if they have the same
928
941
arity and contained types.
929
942
930
943
``` rust
931
- let mut x = (1i , 2i );
932
- let y = (2i , 3i );
944
+ let mut x = (1i , 2i ); // x: (int, int)
945
+ let y = (2i , 3i ); // y: (int, int)
933
946
934
947
x = y ;
935
948
```
@@ -981,7 +994,7 @@ struct Point {
981
994
}
982
995
983
996
fn main () {
984
- let origin = Point { x : 0i , y : 0i };
997
+ let origin = Point { x : 0i , y : 0i }; // origin: Point
985
998
986
999
println! (" The origin is at ({}, {})" , origin . x, origin . y);
987
1000
}
@@ -1101,7 +1114,7 @@ fn main() {
1101
1114
let x = 5i;
1102
1115
let y = 10i;
1103
1116
1104
- let ordering = cmp(x, y);
1117
+ let ordering = cmp(x, y); // ordering: Ordering
1105
1118
1106
1119
if ordering == Less {
1107
1120
println!("less");
@@ -1388,7 +1401,7 @@ Instead, it looks like this:
1388
1401
1389
1402
``` {rust}
1390
1403
for x in range(0i, 10i) {
1391
- println!("{}", x);
1404
+ println!("{}", x); // x: int
1392
1405
}
1393
1406
```
1394
1407
@@ -1423,8 +1436,8 @@ The other kind of looping construct in Rust is the `while` loop. It looks like
1423
1436
this:
1424
1437
1425
1438
``` {rust}
1426
- let mut x = 5u;
1427
- let mut done = false;
1439
+ let mut x = 5u; // mut x: uint
1440
+ let mut done = false; // mut done: bool
1428
1441
1429
1442
while !done {
1430
1443
x += x - 3;
@@ -1520,7 +1533,7 @@ The first kind is a `&str`. This is pronounced a 'string slice.' String literals
1520
1533
are of the type ` &str ` :
1521
1534
1522
1535
``` {rust}
1523
- let string = "Hello there.";
1536
+ let string = "Hello there."; // string: &str
1524
1537
```
1525
1538
1526
1539
This string is statically allocated, meaning that it's saved inside our
@@ -1532,7 +1545,7 @@ A `String`, on the other hand, is an in-memory string. This string is
1532
1545
growable, and is also guaranteed to be UTF-8.
1533
1546
1534
1547
``` {rust}
1535
- let mut s = "Hello".to_string();
1548
+ let mut s = "Hello".to_string(); // mut s: String
1536
1549
println!("{}", s);
1537
1550
1538
1551
s.push_str(", world.");
@@ -1588,16 +1601,19 @@ things. The most basic is the **array**, a fixed-size list of elements of the
1588
1601
same type. By default, arrays are immutable.
1589
1602
1590
1603
``` {rust}
1591
- let a = [1i, 2i, 3i];
1592
- let mut m = [1i, 2i, 3i];
1604
+ let a = [1i, 2i, 3i]; // a: [int, ..3]
1605
+ let mut m = [1i, 2i, 3i]; // mut m: [int, ..3]
1593
1606
```
1594
1607
1595
1608
You can create an array with a given number of elements, all initialized to the
1596
1609
same value, with ` [val, ..N] ` syntax. The compiler ensures that arrays are
1597
1610
always initialized.
1598
1611
1612
+ There's a shorthand for initializing each element of an array to the same
1613
+ value. In this example, each element of ` a ` will be initialized to ` 0i ` :
1614
+
1599
1615
``` {rust}
1600
- let a = [0i, ..20]; // Shorthand for array of 20 elements all initialized to 0
1616
+ let a = [0i, ..20]; // a: [int, ..20]
1601
1617
```
1602
1618
1603
1619
Arrays have type ` [T,..N] ` . We'll talk about this ` T ` notation later, when we
@@ -1608,7 +1624,7 @@ You can get the number of elements in an array `a` with `a.len()`, and use
1608
1624
number in order:
1609
1625
1610
1626
``` {rust}
1611
- let a = [1i, 2, 3]; // Only the first item needs a type suffix
1627
+ let a = [1i, 2, 3]; // Only the first item needs a type suffix
1612
1628
1613
1629
println!("a has {} elements", a.len());
1614
1630
for e in a.iter() {
@@ -1619,7 +1635,7 @@ for e in a.iter() {
1619
1635
You can access a particular element of an array with ** subscript notation** :
1620
1636
1621
1637
``` {rust}
1622
- let names = ["Graydon", "Brian", "Niko"];
1638
+ let names = ["Graydon", "Brian", "Niko"]; // names: [&str, 3]
1623
1639
1624
1640
println!("The second name is: {}", names[1]);
1625
1641
```
@@ -1637,7 +1653,7 @@ later). Vectors are to arrays what `String` is to `&str`. You can create them
1637
1653
with the ` vec! ` macro:
1638
1654
1639
1655
``` {rust}
1640
- let v = vec![1i, 2, 3];
1656
+ let v = vec![1i, 2, 3]; // v: Vec<int>
1641
1657
```
1642
1658
1643
1659
(Notice that unlike the ` println! ` macro we've used in the past, we use square
@@ -1648,8 +1664,10 @@ You can get the length of, iterate over, and subscript vectors just like
1648
1664
arrays. In addition, (mutable) vectors can grow automatically:
1649
1665
1650
1666
``` {rust}
1651
- let mut nums = vec![1i, 2, 3];
1667
+ let mut nums = vec![1i, 2, 3]; // mut nums: Vec<int>
1668
+
1652
1669
nums.push(4);
1670
+
1653
1671
println!("The length of nums is now {}", nums.len()); // Prints 4
1654
1672
```
1655
1673
@@ -1823,10 +1841,12 @@ use std::io;
1823
1841
fn main() {
1824
1842
println!("Type something!");
1825
1843
1826
- let input = io::stdin()
1827
- .read_line()
1828
- .ok()
1829
- .expect("Failed to read line");
1844
+ // here, we'll show the types at each step
1845
+
1846
+ let input = io::stdin() // std::io::stdio::StdinReader
1847
+ .read_line() // IoResult<String>
1848
+ .ok() // Option<String>
1849
+ .expect("Failed to read line"); // String
1830
1850
1831
1851
println!("{}", input);
1832
1852
}
@@ -1969,7 +1989,7 @@ use std::rand;
1969
1989
fn main() {
1970
1990
println!("Guess the number!");
1971
1991
1972
- let secret_number = (rand::random() % 100i) + 1i;
1992
+ let secret_number = (rand::random() % 100i) + 1i; // secret_number: int
1973
1993
1974
1994
println!("The secret number is: {}", secret_number);
1975
1995
@@ -2262,8 +2282,8 @@ In this case, we say `x` is a `uint` explicitly, so Rust is able to properly
2262
2282
tell ` random()` what to generate. In a similar fashion, both of these work:
2263
2283
2264
2284
` ` ` {rust,ignore}
2265
- let input_num = from_str::< uint>( " 5" ) ;
2266
- let input_num: Option< uint> = from_str(" 5" );
2285
+ let input_num = from_str::< uint>( " 5" ) ; // input_num: Option < uint >
2286
+ let input_num: Option< uint> = from_str(" 5" ); // input_num: Option < uint >
2267
2287
` ` `
2268
2288
2269
2289
Anyway, with us now converting our input to a number, our code looks like this:
0 commit comments