Advertisement
cwchen

[Rust] Vector manipulation demo.

Aug 22nd, 2017
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.61 KB | None | 0 0
  1. fn main() {
  2.     // Declare an empty vector
  3.     let mut vec = Vec::new();
  4.  
  5.     // Append data to the tail of the vector
  6.     vec.push(1);
  7.     vec.push(2);
  8.     vec.push(3);
  9.  
  10.     // Get the length of the vector
  11.     assert_eq!(vec.len(), 3);
  12.  
  13.     // Pop data from the tail of the vector
  14.     let popped = vec.pop().unwrap();
  15.     assert_eq!(popped, 3);
  16.     assert_eq!(vec, vec![1, 2]);
  17.  
  18.     // Insert data into the middle of the vector
  19.     vec.insert(1, 99);
  20.     assert_eq!(vec, vec![1, 99, 2]);
  21.  
  22.     // Remove data from the middle of the vector
  23.     let removed = vec.remove(1);
  24.     assert_eq!(removed, 99);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement