- Learning Rust
- Paul Johnson Vesa Kaihlavirta
- 117字
- 2021-07-02 23:07:18
Vectors
While arrays are simple to use, they have a single big disadvantage: they cannot be resized. The vector (Vec) acts in a way similar to a List in C#. It is also a generic type, as Vec itself is actually Vec<T>.
The Vec type is found in the standard library (std::vec).
To create a vector, we use something akin to either of the following:
let mut my_vector: Vec<f32> = Vec::new(); // explicit definition
Or this:
let mut my_alt_vector = vec![4f32, 3.14, 6.28, 13.54, 27.08];
The f32 within the Vec macro tells the compiler that the type for the vector is f32. The f32 can be omitted, as the compiler can determine the type for the vector.
推薦閱讀