- Hands-On Data Structures and Algorithms with Rust
- Claus Matzinger
- 240字
- 2021-07-02 14:11:49
Sized and unsized
For the compiler to translate written code into a binary format, it's necessary to know each type's size. As we discussed earlier, the size is important so that we can put other types on top when working on the stack, something that is easy if the size doesn't change with respect to the data it contains (a sized type). The best example for this is u32: it uses 32 bits (or 4 bytes), regardless of whether you store 0 or 10000900.
This isn't the case when the type is unsized or dynamically sized, the best example being a str. Depending on the number of characters, this type's size will vary considerably, and which is why instances are usually encountered in the form of slices.
Slices work around the size issue by storing a fixed-size reference (&str) to the heap-allocated value, along with its length in bytes. Similar to pointers, this is a fixed-size view into a previously-unsized value. Every time a pointer of some kind (&, Rc, Box, Cell, and so on) is created, the reference is stored alongside the length and some (fixed size) metadata. The knowledge of sized versus unsized is especially useful when the type is previously unknown—when working with Rust's generics, for example.
- 大規模數據分析和建模:基于Spark與R
- Access 2016數據庫教程(微課版·第2版)
- 分布式數據庫系統:大數據時代新型數據庫技術(第3版)
- Learning JavaScriptMVC
- 工業大數據分析算法實戰
- SQL查詢:從入門到實踐(第4版)
- 數據化網站運營深度剖析
- Access 2016數據庫技術及應用
- Remote Usability Testing
- 智能數據時代:企業大數據戰略與實戰
- The Game Jam Survival Guide
- 深入淺出 Hyperscan:高性能正則表達式算法原理與設計
- 大數據架構商業之路:從業務需求到技術方案
- 計算機應用基礎教程上機指導與習題集(微課版)
- 數據庫應用系統技術