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