Skip to content

Add more explanation on vec type #31850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down