misc/process/wait.html)
    9. [ **20.6.** Filesystem Operations ](../std_misc/fs.html)
    10. [ **20.7.** Program arguments ](../std_misc/arg.html) â± 
    11.       1. [ **20.7.1.** Argument parsing ](../std_misc/arg/matching.html)
    12. [ **20.8.** Foreign Function Interface ](../std_misc/ffi.html)
  41. [ **21.** Testing ](../testing.html) â± 
  42.     1. [ **21.1.** Unit testing ](../testing/unit_testing.html)
    2. [ **21.2.** Documentation testing ](../testing/doc_testing.html)
    3. [ **21.3.** Integration testing ](../testing/integration_testing.html)
    4. [ **21.4.** Dev-dependencies ](../testing/dev_dependencies.html)
  43. [ **22.** Unsafe Operations ](../unsafe.html) â± 
  44.     1. [ **22.1.** Inline assembly ](../unsafe/asm.html)
  45. [ **23.** Compatibility ](../compatibility.html) â± 
  46.     1. [ **23.1.** Raw identifiers ](../compatibility/raw_identifiers.html)
  47. [ **24.** Meta ](../meta.html) â± 
  48.     1. [ **24.1.** Documentation ](../meta/doc.html)
    2. [ **24.2.** Playground ](../meta/playground.html)

__ __

  * Light (default) 
  * Rust 
  * Coal 
  * Navy 
  * Ayu 

__

#  Rust By Example

[ __ ](../print.html "Print this book") [ __ ](https://github.com/rust-
lang/rust-by-example "Git repository") [ __ ](https://github.com/rust-
lang/rust-by-example/edit/master/src/std/vec.md "Suggest an edit")

#  Vectors

Vectors are re-sizable arrays. Like slices, their size is not known at compile
time, but they can grow or shrink at any time. A vector is represented using 3
parameters:

  * pointer to the data 
  * length 
  * capacity 

The capacity indicates how much memory is reserved for the vector. The vector
can grow as long as the length is smaller than the capacity. When this
threshold needs to be surpassed, the vector is reallocated with a larger
capacity.

    
    
    fn main() {
        // Iterators can be collected into vectors
        let collected_iterator: Vec<i32> = (0..10).collect();
        println!("Collected (0..10) into: {:?}", collected_iterator);
    
        // The `vec!` macro can be used to initialize a vector
        let mut xs = vec![1i32, 2, 3];
        println!("Initial vector: {:?}", xs);
    
        // Insert new element at the end of the vector
        println!("Push 4 into the vector");
        xs.push(4);
        println!("Vector: {:?}", xs);
    
        // Error! Immutable vectors can't grow
        collected_iterator.push(0);
        // FIXME ^ Comment out this line
    
        // The `len` method yields the number of elements currently stored in a vector
        println!("Vector length: {}", xs.len());
    
        // Indexing is done using the square brackets (indexing starts at 0)
        println!("Second element: {}", xs[1]);
    
        // `pop` removes the last element from the vector and returns it
        println!("Pop last element: {:?}", xs.pop());
    
        // Out of bounds indexing yields a panic
        println!("Fourth element: {}", xs[3]);
        // FIXME ^ Comment out this line
    
        // `Vector`s can be easily iterated over
        println!("Contents of xs:");
        for x in xs.iter() {
            println!("> {}", x);
        }
    
        // A `Vector` can also be iterated over while the iteration
        // count is enumerated in a separate variable (`i`)
        for (i, x) in xs.iter().enumerate() {
            println!("In position {} we have value {}", i, x);
        }
    
        // Thanks to `iter_mut`, mutable `Vector`s can also be iterated
        // over in a way that allows modifying each value
        for x in xs.iter_mut() {
            *x *= 3;
        }
        println!("Updated vector: {:?}", xs);
    }
    

More ` Vec ` methods can be found under the [ std::vec ](https://doc.rust-
lang.org/std/vec/) module

[ __ ](../std/box.html "Previous chapter") [ __ ](../std/str.html "Next
chapter")

[ __ ](../std/box.html "Previous chapter") [ __ ](../std/str.html "Next
chapter")