Skip to content

Commit be91280

Browse files
committed
---
yaml --- r: 132663 b: refs/heads/dist-snap c: 8175cba h: refs/heads/master i: 132661: 7504c23 132659: 6b296c7 132655: 76e7cbc v: v3
1 parent 82a4754 commit be91280

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: 457a3c991d79b971be07fce75f9d0c12848fb37c
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
9-
refs/heads/dist-snap: 6faad3ec3aba8e9ea7c68331b7c6561890929658
9+
refs/heads/dist-snap: 8175cba597a18875ae04071b345479d4311ed02e
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1212
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/src/doc/guide.md

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ computer science: naming things, cache invalidation, and off-by-one errors."
13411341
The joke, of course, being that the setup says "two hard problems" but then
13421342
lists three things. This happens quite a bit with "C style" `for` loops.
13431343

1344-
We'll talk more about `for` when we cover **vector**s, later in the Guide.
1344+
We'll talk more about `for` when we cover **iterator**s, later in the Guide.
13451345

13461346
## `while`
13471347

@@ -1427,11 +1427,6 @@ for x in range(0i, 10i) {
14271427

14281428
Both `continue` and `break` are valid in both kinds of loops.
14291429

1430-
We have now learned all of the most basic Rust concepts. We're ready to start
1431-
building our guessing game, but we need to know how to do one last thing first:
1432-
get input from the keyboard. You can't have a guessing game without the ability
1433-
to guess!
1434-
14351430
# Strings
14361431

14371432
Strings are an important concept for any programmer to master. Rust's string
@@ -1512,6 +1507,76 @@ low-level details matter, they really matter. Just remember that `String`s
15121507
allocate memory and control their data, while `&str`s are a reference to
15131508
another string, and you'll be all set.
15141509

1510+
# Vectors
1511+
1512+
Like many programming languages, Rust has a list type for when you want a list
1513+
of things. But similar to strings, Rust has different types to represent this
1514+
idea: `Vec<T>` (a 'vector'), `[T, .. N]` (an 'array'), and `&[T]` (a 'slice').
1515+
Whew!
1516+
1517+
Vectors are similar to `String`s: they have a dynamic length, and they
1518+
allocate enough memory to fit. You can create a vector with the `vec!` macro:
1519+
1520+
```{rust}
1521+
let nums = vec![1i, 2i, 3i];
1522+
```
1523+
1524+
Notice that unlike the `println!` macro we've used in the past, we use square
1525+
brackets (`[]`) with `vec!`. Rust allows you to use either in either situation,
1526+
this is just convention.
1527+
1528+
You can create an array with just square brackets:
1529+
1530+
```{rust}
1531+
let nums = [1i, 2i, 3i];
1532+
```
1533+
1534+
So what's the difference? An array has a fixed size, so you can't add or
1535+
subtract elements:
1536+
1537+
```{rust,ignore}
1538+
let mut nums = vec![1i, 2i, 3i];
1539+
nums.push(4i); // works
1540+
1541+
let mut nums = [1i, 2i, 3i];
1542+
nums.push(4i); // error: type `[int, .. 3]` does not implement any method
1543+
// in scope named `push`
1544+
```
1545+
1546+
The `push()` method lets you append a value to the end of the vector. But
1547+
since arrays have fixed sizes, adding an element doesn't make any sense.
1548+
You can see how it has the exact type in the error message: `[int, .. 3]`.
1549+
An array of `int`s, with length 3.
1550+
1551+
Similar to `&str`, a slice is a reference to another array. We can get a
1552+
slice from a vector by using the `as_slice()` method:
1553+
1554+
```{rust}
1555+
let vec = vec![1i, 2i, 3i];
1556+
let slice = vec.as_slice();
1557+
```
1558+
1559+
All three types implement an `iter()` method, which returns an iterator. We'll
1560+
talk more about the details of iterators later, but for now, the `iter()` method
1561+
allows you to write a `for` loop that prints out the contents of a vector, array,
1562+
or slice:
1563+
1564+
```{rust}
1565+
let vec = vec![1i, 2i, 3i];
1566+
1567+
for i in vec.iter() {
1568+
println!("{}", i);
1569+
}
1570+
```
1571+
1572+
This code will print each number in order, on its own line.
1573+
1574+
There's a whole lot more to vectors, but that's enough to get started. We have
1575+
now learned all of the most basic Rust concepts. We're ready to start building
1576+
our guessing game, but we need to know how to do one last thing first: get
1577+
input from the keyboard. You can't have a guessing game without the ability to
1578+
guess!
1579+
15151580
# Standard Input
15161581

15171582
Getting input from the keyboard is pretty easy, but uses some things

0 commit comments

Comments
 (0)