November 2017
Intermediate to advanced
264 pages
5h 45m
English
Often it is more practical to work with a kind of array than can grow (or shrink) in size because it is allocated on the heap. Rust provides this through the vector type Vec, from the module std::vec. This is a generic type, which means that the items can have any type T, where T is specified in the code; for example we can have Vec<i32> or Vec<&str> vector types. To indicate that it is a generic type, it is written as Vec<T>, where all elements must be of the same type T.
We can make a vector in either of two ways, with the function new() or with the macro vec!:
let mut numbers: Vec<i32> = Vec::new(); let mut magic_numbers = vec![7i32, 42, 47, 45, 54];
In the first case the type is indicated explicitly with the vector type Vec<i32 ...
Read now
Unlock full access