- 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.
- 數(shù)據(jù)分析實(shí)戰(zhàn):基于EXCEL和SPSS系列工具的實(shí)踐
- 算法競賽入門經(jīng)典:習(xí)題與解答
- Creating Mobile Apps with Sencha Touch 2
- 分布式數(shù)據(jù)庫系統(tǒng):大數(shù)據(jù)時(shí)代新型數(shù)據(jù)庫技術(shù)(第3版)
- Redis應(yīng)用實(shí)例
- Hadoop大數(shù)據(jù)實(shí)戰(zhàn)權(quán)威指南(第2版)
- 中國數(shù)字流域
- MATLAB Graphics and Data Visualization Cookbook
- 改變未來的九大算法
- 區(qū)塊鏈+:落地場景與應(yīng)用實(shí)戰(zhàn)
- Expert Python Programming(Third Edition)
- Spring MVC Beginner’s Guide
- 數(shù)據(jù)挖掘競賽實(shí)戰(zhàn):方法與案例
- 實(shí)現(xiàn)領(lǐng)域驅(qū)動設(shè)計(jì)
- Scratch 2.0 Game Development HOTSHOT