Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Remove the last element using pop, reduce capacity

  • pop
  • len
  • capacity
  • shrink_to_fit
fn main() {
    let mut animals = vec![
        String::from("dog"),
        String::from("cat"),
        String::from("camel"),
        String::from("crab"),
        String::from("snake"),
    ];
    println!("{} {}", animals.len(), animals.capacity());
    match animals.pop() {
        None => println!("No animals"),
        Some(last) => println!("last: {last}"),
    }
    println!("{} {}", animals.len(), animals.capacity());

    animals.shrink_to_fit();
    println!("{} {}", animals.len(), animals.capacity());
}
5 5
last: snake
4 5
4 4