[Rust] Vectors in Rust

In this lesson you'll learn about Vec<T>, or Vectors. Vectors are like Arrays, a collection of values of the same type, but as opposed to Arrays, Vectors can change in size.

#[allow(warnings)]

fn main() {
    let mut numbers = vec![1, 4];
    numbers.push(3);
    numbers.push(2);
    
    for number in &numbers {
        println!("{}", number)
    }
}

 

Another way to create Vector:

let mut numbers = Vec::new();
numbers.push(1);
numbers.push(2);

 

posted @ 2024-02-20 22:36  Zhentiw  阅读(3)  评论(0编辑  收藏  举报