Skip to content

Commit 86a858a

Browse files
committed
Rollup merge of rust-lang#25087 - nham:improve_vec_docs, r=Gankro
This commit does two things: it adds an example for indexing vectors, and it changes the \"Examples\" section to use full sentences. This change was spurred by someone in the #rust IRC channel asking if there was a `.set()` method for changing the `i`-th value of a vector (they had missed that `Vec` implements `IndexMut`, which is easy to do if you're not aware of that trait).
2 parents 7413052 + 51463c3 commit 86a858a

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/libcollections/vec.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,43 @@
1515
//!
1616
//! # Examples
1717
//!
18-
//! Explicitly creating a `Vec<T>` with `new()`:
18+
//! You can explicitly create a `Vec<T>` with `new()`:
1919
//!
2020
//! ```
2121
//! let xs: Vec<i32> = Vec::new();
2222
//! ```
2323
//!
24-
//! Using the `vec!` macro:
24+
//! ...or by using the `vec!` macro:
2525
//!
2626
//! ```
2727
//! let ys: Vec<i32> = vec![];
2828
//!
2929
//! let zs = vec![1i32, 2, 3, 4, 5];
3030
//! ```
3131
//!
32-
//! Push:
32+
//! You can `push` values onto the end of a vector (which will grow the vector as needed):
3333
//!
3434
//! ```
3535
//! let mut xs = vec![1i32, 2];
3636
//!
3737
//! xs.push(3);
3838
//! ```
3939
//!
40-
//! And pop:
40+
//! Popping values works in much the same way:
4141
//!
4242
//! ```
4343
//! let mut xs = vec![1i32, 2];
4444
//!
4545
//! let two = xs.pop();
4646
//! ```
47+
//!
48+
//! Vectors also support indexing (through the `Index` and `IndexMut` traits):
49+
//!
50+
//! ```
51+
//! let mut xs = vec![1i32, 2, 3];
52+
//! let three = xs[2];
53+
//! xs[1] = xs[1] + 5;
54+
//! ```
4755
4856
#![stable(feature = "rust1", since = "1.0.0")]
4957

0 commit comments

Comments
 (0)