Skip to content

Commit 31b240d

Browse files
committed
Add comments with type annotations.
This will hopefully help people with their first steps in Rust. Fixes #16143.
1 parent c38e73f commit 31b240d

File tree

1 file changed

+46
-26
lines changed

1 file changed

+46
-26
lines changed

src/doc/guide.md

+46-26
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,19 @@ let x: int = 5;
417417
If I asked you to read this out loud to the rest of the class, you'd say "`x`
418418
is a binding with the type `int` and the value `five`."
419419

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+
420433
By default, bindings are **immutable**. This code will not compile:
421434

422435
```{ignore}
@@ -435,7 +448,7 @@ error: re-assignment of immutable variable `x`
435448
If you want a binding to be mutable, you can use `mut`:
436449

437450
```{rust}
438-
let mut x = 5i;
451+
let mut x = 5i; // mut x: int
439452
x = 10i;
440453
```
441454

@@ -583,15 +596,15 @@ let y = if x == 5i {
583596
10i
584597
} else {
585598
15i
586-
};
599+
}; // y: int
587600
```
588601

589602
Which we can (and probably should) write like this:
590603

591604
```{rust}
592605
let x = 5i;
593606
594-
let y = if x == 5i { 10i } else { 15i };
607+
let y = if x == 5i { 10i } else { 15i }; // y: int
595608
```
596609

597610
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
927940
arity and contained types.
928941

929942
```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)
932945

933946
x = y;
934947
```
@@ -980,7 +993,7 @@ struct Point {
980993
}
981994

982995
fn main() {
983-
let origin = Point { x: 0i, y: 0i };
996+
let origin = Point { x: 0i, y: 0i }; // origin: Point
984997

985998
println!("The origin is at ({}, {})", origin.x, origin.y);
986999
}
@@ -1100,7 +1113,7 @@ fn main() {
11001113
let x = 5i;
11011114
let y = 10i;
11021115
1103-
let ordering = cmp(x, y);
1116+
let ordering = cmp(x, y); // ordering: Ordering
11041117
11051118
if ordering == Less {
11061119
println!("less");
@@ -1387,7 +1400,7 @@ Instead, it looks like this:
13871400

13881401
```{rust}
13891402
for x in range(0i, 10i) {
1390-
println!("{}", x);
1403+
println!("{}", x); // x: int
13911404
}
13921405
```
13931406

@@ -1422,8 +1435,8 @@ The other kind of looping construct in Rust is the `while` loop. It looks like
14221435
this:
14231436

14241437
```{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
14271440
14281441
while !done {
14291442
x += x - 3;
@@ -1519,7 +1532,7 @@ The first kind is a `&str`. This is pronounced a 'string slice.' String literals
15191532
are of the type `&str`:
15201533

15211534
```{rust}
1522-
let string = "Hello there.";
1535+
let string = "Hello there."; // string: &str
15231536
```
15241537

15251538
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
15311544
growable, and is also guaranteed to be UTF-8.
15321545

15331546
```{rust}
1534-
let mut s = "Hello".to_string();
1547+
let mut s = "Hello".to_string(); // mut s: String
15351548
println!("{}", s);
15361549
15371550
s.push_str(", world.");
@@ -1587,16 +1600,19 @@ things. The most basic is the **array**, a fixed-size list of elements of the
15871600
same type. By default, arrays are immutable.
15881601

15891602
```{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]
15921605
```
15931606

15941607
You can create an array with a given number of elements, all initialized to the
15951608
same value, with `[val, ..N]` syntax. The compiler ensures that arrays are
15961609
always initialized.
15971610

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+
15981614
```{rust}
1599-
let a = [0i, ..20]; // Shorthand for array of 20 elements all initialized to 0
1615+
let a = [0i, ..20]; // a: [int, ..20]
16001616
```
16011617

16021618
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
16071623
number in order:
16081624

16091625
```{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
16111627
16121628
println!("a has {} elements", a.len());
16131629
for e in a.iter() {
@@ -1618,7 +1634,7 @@ for e in a.iter() {
16181634
You can access a particular element of an array with **subscript notation**:
16191635

16201636
```{rust}
1621-
let names = ["Graydon", "Brian", "Niko"];
1637+
let names = ["Graydon", "Brian", "Niko"]; // names: [&str, 3]
16221638
16231639
println!("The second name is: {}", names[1]);
16241640
```
@@ -1636,7 +1652,7 @@ later). Vectors are to arrays what `String` is to `&str`. You can create them
16361652
with the `vec!` macro:
16371653

16381654
```{rust}
1639-
let v = vec![1i, 2, 3];
1655+
let v = vec![1i, 2, 3]; // v: Vec<int>
16401656
```
16411657

16421658
(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
16471663
arrays. In addition, (mutable) vectors can grow automatically:
16481664

16491665
```{rust}
1650-
let mut nums = vec![1i, 2, 3];
1666+
let mut nums = vec![1i, 2, 3]; // mut nums: Vec<int>
1667+
16511668
nums.push(4);
1669+
16521670
println!("The length of nums is now {}", nums.len()); // Prints 4
16531671
```
16541672

@@ -1822,10 +1840,12 @@ use std::io;
18221840
fn main() {
18231841
println!("Type something!");
18241842
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
18291849
18301850
println!("{}", input);
18311851
}
@@ -1968,7 +1988,7 @@ use std::rand;
19681988
fn main() {
19691989
println!("Guess the number!");
19701990
1971-
let secret_number = (rand::random() % 100i) + 1i;
1991+
let secret_number = (rand::random() % 100i) + 1i; // secret_number: int
19721992
19731993
println!("The secret number is: {}", secret_number);
19741994
@@ -2261,8 +2281,8 @@ In this case, we say `x` is a `uint` explicitly, so Rust is able to properly
22612281
tell `random()` what to generate. In a similar fashion, both of these work:
22622282

22632283
```{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>
22662286
```
22672287

22682288
Anyway, with us now converting our input to a number, our code looks like this:

0 commit comments

Comments
 (0)