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

Accessing the last element of a vector

  • len

  • last

  • TODO

  • Unlike Python and Perl, rust won't let us use a negative index in a vector so we won't be able to access the last element using vector_name[-1]

  • We can either use vector_name.len()-1 or

  • We can use vector_name.last(), but in this case we get an Option that can be None as well.

  • If we access a seemingly arbitrary element that we calculated using vector_name.len()-1 then either we get back a value or Rust will panic if we gave an index too big.

  • On the other hand using last we are more protected. In that case we either get a value or None if the vector was empty.

fn main() {
    let planets: Vec<&str> = vec!["Mercury", "Venus", "Earth"];
    println!("{}", planets.len());
    println!("{}", planets[planets.len() - 1]);
    // println!("{}", planets[ 5 ]);  // thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 5'
    println!("{}", planets.last().unwrap());

    let planets: Vec<&str> = vec![];
    //println!("{}", planets[ planets.len()-1 ]);  // thread 'main' panicked at 'attempt to subtract with overflow'
    println!("{:?}", planets.last()); // None
}
3
Earth
Earth
None