Vectors allow you to store more than one value in a single data
structure that puts all the values next to each other in memory. Vectors
can only store values of the same type. They are useful when you have a
list of items, such as the lines of text in a file or the prices of
items in a shopping cart.
Creating a New Vector
vec! macro
Rust can often infer the type of value you want to store in the Vector.
For convenience one can also use the vec!
macro.
Updating a Vector
Dropping a Vector
Like other structs, vectors are freed when
they go out of scope, along with the vector contents:
Read Vector elements
There are two ways to reference a value stored in a vector:
Using
get
together with
match
seems preferable, as trying to get non existant elements with method #1
will cause an `index out of bounds` panic:
When the get method is passed an index that is outside the vector, it
returns None without panicking.