about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2016-02-25 04:21:11 +0530
committerManish Goregaokar <manishsmail@gmail.com>2016-02-25 04:21:11 +0530
commit901eca9e6ce7a527e1014fda3301e6ddf1365096 (patch)
tree5455708fefb6d243b9bc827aeb0ebb40c6f5826c
parentb28debd91ca23afcc9485f16690de052bd181e58 (diff)
parentdf33fc352d7b6aecc0ccadbfaf56484991cb80be (diff)
downloadrust-901eca9e6ce7a527e1014fda3301e6ddf1365096.tar.gz
rust-901eca9e6ce7a527e1014fda3301e6ddf1365096.zip
Rollup merge of #31850 - GuillaumeGomez:vec-doc, r=steveklabnik
r? @steveklabnik
cc @mbrubeck
-rw-r--r--src/libcollections/vec.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs
index 49c35520833..ae442e155c0 100644
--- a/src/libcollections/vec.rs
+++ b/src/libcollections/vec.rs
@@ -135,6 +135,49 @@ use super::range::RangeArgument;
 /// }
 /// ```
 ///
+/// # Indexing
+///
+/// The Vec type allows to access values by index, because it implements the
+/// `Index` trait. An example will be more explicit:
+///
+/// ```
+/// let v = vec!(0, 2, 4, 6);
+/// println!("{}", v[1]); // it will display '2'
+/// ```
+///
+/// However be careful: if you try to access an index which isn't in the Vec,
+/// your software will panic! You cannot do this:
+///
+/// ```ignore
+/// let v = vec!(0, 2, 4, 6);
+/// println!("{}", v[6]); // it will panic!
+/// ```
+///
+/// In conclusion: always check if the index you want to get really exists
+/// before doing it.
+///
+/// # Slicing
+///
+/// A Vec can be mutable. Slices, on the other hand, are read-only objects.
+/// To get a slice, use "&". Example:
+///
+/// ```
+/// fn read_slice(slice: &[usize]) {
+///     // ...
+/// }
+///
+/// let v = vec!(0, 1);
+/// read_slice(&v);
+///
+/// // ... and that's all!
+/// // you can also do it like this:
+/// let x : &[usize] = &v;
+/// ```
+///
+/// In Rust, it's more common to pass slices as arguments rather than vectors
+/// when you just want to provide a read access. The same goes for String and
+/// &str.
+///
 /// # Capacity and reallocation
 ///
 /// The capacity of a vector is the amount of space allocated for any future