Skip to content

Commit 3f9ff2e

Browse files
committed
Guide: array subscript notation
1 parent c7d0b52 commit 3f9ff2e

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/doc/guide.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,19 @@ for i in vec.iter() {
15711571

15721572
This code will print each number in order, on its own line.
15731573

1574+
You can access a particular element of a vector, array, or slice by using
1575+
**subscript notation**:
1576+
1577+
```{rust}
1578+
let names = ["Graydon", "Brian", "Niko"];
1579+
1580+
println!("The second name is: {}", names[1]);
1581+
```
1582+
1583+
These subscripts start at zero, like in most programming languages, so the
1584+
first name is `names[0]` and the second name is `names[1]`. The above example
1585+
prints `The second name is Brian`.
1586+
15741587
There's a whole lot more to vectors, but that's enough to get started. We have
15751588
now learned all of the most basic Rust concepts. We're ready to start building
15761589
our guessing game, but we need to know how to do one last thing first: get

0 commit comments

Comments
 (0)