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

Fixed vector of numbers

  • vec!

  • len

  • A Vec vector is a series of values of the same type.

  • We can initialize a vector using the vec! macro.

  • We can get the length of the vector using the len method.

  • We cannot print a vector with the simle {} placeholder because Display is not implemented for it.

  • However we can use the {:?} or the {:#?} placeholders.

  • By default vectors are immutable.

fn main() {
    let numbers = vec![10, 11, 12];
    println!("{}", numbers.len());

    // println!("{}", numbers);
    // `Vec<{integer}>` doesn't implement `std::fmt::Display`
    // `Vec<{integer}>` cannot be formatted with the default formatter

    println!("{:?}", numbers);
    println!("{:#?}", numbers);
}
3
[10, 11, 12]
[
    10,
    11,
    12,
]