- Rust Programming Cookbook
- Claus Matzinger
- 279字
- 2021-06-24 12:27:46
How it works...
Sequence types are compound types that allocate a continuous part of the memory for faster and easier access. Vec<T> creates a simple, heap-allocated version of an array that grows (and shrinks) dynamically (step 4).
The original array (step 2) allocates memory on the stack and has to have a known size at compile time, which is a significant factor in using it. Both can be iterated and viewed using slices (https://doc.rust-lang.org/book/ch04-03-slices.html).
Tuples (step 3) are a different beast since they don't lend themselves to slices and are more a group of variables that have a semantic relationship—like a point in a two-dimensional space. Another use case is to return more than one variable to the caller of a function without the use of an additional struct or misusing a collection type.
Sequences in Rust are special because of the low overhead they produce. The size of Vec<T> is a pointer to an n * size of T memory on the heap, along with the size of the allocated memory, and how much of that is used. For arrays, the capacity is the current size (which the compiler can fill in during compilation), and tuples are more or less syntactic sugar on top of three distinct variables. Each of the three types provides convenience functions to change the contents—and, in the case of Vec<T>, the size of the collection. We recommend taking a close look at the tests and their comments to find out more about each type.
We have covered the basics of sequences in Rust, so let's move on to the next recipe.
- Dynamics 365 for Finance and Operations Development Cookbook(Fourth Edition)
- JavaScript高效圖形編程
- Web Scraping with Python
- Unity 2020 Mobile Game Development
- Mastering Unity Shaders and Effects
- QGIS By Example
- Access 2010數據庫應用技術(第2版)
- 執劍而舞:用代碼創作藝術
- 時空數據建模及其應用
- 物聯網系統架構設計與邊緣計算(原書第2版)
- 從零開始學Selenium自動化測試:基于Python:視頻教學版
- UI設計基礎培訓教程(全彩版)
- Python 3快速入門與實戰
- JavaEE架構與程序設計
- Mapping with ArcGIS Pro