Vector length and capacity



examples/vectors/with-capacity/src/main.rs
fn main() {
    // Create vector of 5 elements initialized to 0
    let vec = vec![0; 5];
    println!("{:?}", vec);

    // Create vector of 5 elements initialized to 42
    let vec = vec![42; 5];
    println!("{:?}", vec);

    // Creata an empty vector that can hold up to 5 elements.
    let mut vec: Vec<i32> = Vec::with_capacity(5);
    vec.resize(3, 19);
    println!("{:?}", vec);
    println!("len:      {:?}", vec.len());
    println!("capacity: {:?}", vec.capacity());
    println!();

    vec.resize(6, 23);
    println!("{:?}", vec);
    println!("len:      {:?}", vec.len());
    println!("capacity: {:?}", vec.capacity());
    println!();

    let mut vec: Vec<i32> = vec![];
    println!("{:?}", vec);
    println!("len:      {:?}", vec.len());
    println!("capacity: {:?}", vec.capacity());
    println!();

    vec.push(42);
    println!("{:?}", vec);
    println!("len:      {:?}", vec.len());
    println!("capacity: {:?}", vec.capacity());
    println!();
}

[0, 0, 0, 0, 0]
[42, 42, 42, 42, 42]
[19, 19, 19]
len:      3
capacity: 5

[19, 19, 19, 23, 23, 23]
len:      6
capacity: 10

[]
len:      0
capacity: 0

[42]
len:      1
capacity: 4