January 2019
Beginner to intermediate
554 pages
13h 31m
English
Vectors are like arrays, except that their content or length doesn't need to be known in advance and can grow on demand. They are allocated on the heap. They can be created by either calling the Vec::new constructor or by using the vec![] macro:
// vec.rsfn main() { let mut numbers_vec: Vec<u8> = Vec::new(); numbers_vec.push(1); numbers_vec.push(2); let mut vec_with_macro = vec![1]; vec_with_macro.push(2); let _ = vec_with_macro.pop(); // value ignored with `_` let message = if numbers_vec == vec_with_macro { "They are equal" } else { "Nah! They look different to me" }; println!("{} {:?} {:?}", message, numbers_vec, vec_with_macro); }
In the preceding code, we created two vectors, numbers_vec and vec_with_macro, in different ways. ...
Read now
Unlock full access