Skip to content

Commit 3200237

Browse files
committed
Merge pull request #19743 from steveklabnik/gh16143
Add comments with type annotations. Reviewed-by: alexcrichton
2 parents b68e52d + 31b240d commit 3200237

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
@@ -418,6 +418,19 @@ let x: int = 5;
418418
If I asked you to read this out loud to the rest of the class, you'd say "`x`
419419
is a binding with the type `int` and the value `five`."
420420

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

423436
```{ignore}
@@ -436,7 +449,7 @@ error: re-assignment of immutable variable `x`
436449
If you want a binding to be mutable, you can use `mut`:
437450

438451
```{rust}
439-
let mut x = 5i;
452+
let mut x = 5i; // mut x: int
440453
x = 10i;
441454
```
442455

@@ -584,15 +597,15 @@ let y = if x == 5i {
584597
10i
585598
} else {
586599
15i
587-
};
600+
}; // y: int
588601
```
589602

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

592605
```{rust}
593606
let x = 5i;
594607
595-
let y = if x == 5i { 10i } else { 15i };
608+
let y = if x == 5i { 10i } else { 15i }; // y: int
596609
```
597610

598611
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
928941
arity and contained types.
929942

930943
```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)
933946

934947
x = y;
935948
```
@@ -981,7 +994,7 @@ struct Point {
981994
}
982995

983996
fn main() {
984-
let origin = Point { x: 0i, y: 0i };
997+
let origin = Point { x: 0i, y: 0i }; // origin: Point
985998

986999
println!("The origin is at ({}, {})", origin.x, origin.y);
9871000
}
@@ -1101,7 +1114,7 @@ fn main() {
11011114
let x = 5i;
11021115
let y = 10i;
11031116
1104-
let ordering = cmp(x, y);
1117+
let ordering = cmp(x, y); // ordering: Ordering
11051118
11061119
if ordering == Less {
11071120
println!("less");
@@ -1388,7 +1401,7 @@ Instead, it looks like this:
13881401

13891402
```{rust}
13901403
for x in range(0i, 10i) {
1391-
println!("{}", x);
1404+
println!("{}", x); // x: int
13921405
}
13931406
```
13941407

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

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

15221535
```{rust}
1523-
let string = "Hello there.";
1536+
let string = "Hello there."; // string: &str
15241537
```
15251538

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

15341547
```{rust}
1535-
let mut s = "Hello".to_string();
1548+
let mut s = "Hello".to_string(); // mut s: String
15361549
println!("{}", s);
15371550
15381551
s.push_str(", world.");
@@ -1588,16 +1601,19 @@ things. The most basic is the **array**, a fixed-size list of elements of the
15881601
same type. By default, arrays are immutable.
15891602

15901603
```{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]
15931606
```
15941607

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

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

16031619
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
16081624
number in order:
16091625

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

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

16391655
```{rust}
1640-
let v = vec![1i, 2, 3];
1656+
let v = vec![1i, 2, 3]; // v: Vec<int>
16411657
```
16421658

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

16501666
```{rust}
1651-
let mut nums = vec![1i, 2, 3];
1667+
let mut nums = vec![1i, 2, 3]; // mut nums: Vec<int>
1668+
16521669
nums.push(4);
1670+
16531671
println!("The length of nums is now {}", nums.len()); // Prints 4
16541672
```
16551673

@@ -1823,10 +1841,12 @@ use std::io;
18231841
fn main() {
18241842
println!("Type something!");
18251843
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
18301850
18311851
println!("{}", input);
18321852
}
@@ -1969,7 +1989,7 @@ use std::rand;
19691989
fn main() {
19701990
println!("Guess the number!");
19711991
1972-
let secret_number = (rand::random() % 100i) + 1i;
1992+
let secret_number = (rand::random() % 100i) + 1i; // secret_number: int
19731993
19741994
println!("The secret number is: {}", secret_number);
19751995
@@ -2262,8 +2282,8 @@ In this case, we say `x` is a `uint` explicitly, so Rust is able to properly
22622282
tell `random()` what to generate. In a similar fashion, both of these work:
22632283
22642284
```{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>
22672287
```
22682288
22692289
Anyway, with us now converting our input to a number, our code looks like this:

0 commit comments

Comments
 (0)