@@ -1341,7 +1341,7 @@ computer science: naming things, cache invalidation, and off-by-one errors."
1341
1341
The joke, of course, being that the setup says "two hard problems" but then
1342
1342
lists three things. This happens quite a bit with "C style" ` for ` loops.
1343
1343
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.
1345
1345
1346
1346
## ` while `
1347
1347
@@ -1427,11 +1427,6 @@ for x in range(0i, 10i) {
1427
1427
1428
1428
Both ` continue ` and ` break ` are valid in both kinds of loops.
1429
1429
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
-
1435
1430
# Strings
1436
1431
1437
1432
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
1512
1507
allocate memory and control their data, while ` &str ` s are a reference to
1513
1508
another string, and you'll be all set.
1514
1509
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
+
1515
1580
# Standard Input
1516
1581
1517
1582
Getting input from the keyboard is pretty easy, but uses some things
0 commit comments