Skip to content

Commit 15833d9

Browse files
committed
auto merge of #16429 : steveklabnik/rust/guide_vectors, r=cmr
Since #16380 didn't get pulled in yet, I added it in here too. This covers the very, very, very basics of vectors. I wanted to have a section that mentioned them, but I'm unsure what else I should cover. So I just did the absolute simplest things. Feedback very welcome.
2 parents 6faad3e + 8175cba commit 15833d9

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

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)