- Learning Rust
- Paul Johnson Vesa Kaihlavirta
- 152字
- 2021-07-02 23:07:19
Adding and removing from the vector
In a similar fashion to string, it is possible to add and remove from the vector list using the push and pull methods. These add or remove from the top of the vector stack. Consider the following example:
fn main() { let mut my_vec : Vec<i32> = (0..10).collect(); println!("{:?}", my_vec); my_vec.push(13); my_vec.push(21); println!("{:?}", my_vec); let mut twenty_one = my_vec.pop(); // removes the last value println!("twenty_one= {:?}", twenty_one); println!("{:?}", my_vec); }
We create the vector list with values from 0 going up to 10 (so the last value is 9).
The line println!("{:?}", my_vec); outputs the entire contents of my_vec. {:?} is required here due to the type Vec<i32> not implementing certain formatting functionalities.
We then push onto the top of the vector list 13 then 21, display the output on the screen, and then remove the top-most value on the vector list, and output it again.
推薦閱讀
- 數據庫系統原理及MySQL應用教程(第2版)
- Visual Basic編程:從基礎到實踐(第2版)
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第3版)
- C和C++游戲趣味編程
- Building Serverless Architectures
- Mastering Docker
- Oracle實用教程
- 石墨烯改性塑料
- Oracle 12c從入門到精通(視頻教學超值版)
- Clojure for Finance
- Beginning C# 7 Hands-On:The Core Language
- Head First Kotlin程序設計
- 網絡綜合布線與組網實戰指南
- ASP.NET jQuery Cookbook(Second Edition)
- 深度學習的數學:使用Python語言