知识卡片

普通读书笔记卡 · 6074.b

索引类型跟随目标尺寸

理解等级:中级

usize和isize的位宽与目标指针尺寸关联,标准集合长度和索引常使用usize。选它们主要是匹配容量与接口,而不是把固定宽度协议字段也随意改成平台相关整数。跨平台或跨语言边界应另行约定需要的固定范围。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 1. Types Fundamental Types · 查看原书依据

      standard collections return their size as a usize (from .len()), so collection indexing means that usize values are quite common—which is obviously fine from a capacity perspective, as there can’t be more items in an in-memory collection than there are memory addresses on the system. The integral types do give us the first hint that Rust is a stricter world

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Fundamental Types》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u008-ch01-002)。 - 结论依据:原文在“standard collections return their size”附近的定义、例子或代码支撑本卡核心说明:usize和isize的位宽与目标指针尺寸关联,标准集合长度和索引常使用usize。边界按已读前提解释;示例未执行。 - 原始内容:standard collections return their size as a usize (from .len()), so collection indexing means that usize values are quite common—which is obviously fine from a capacity perspective, as there can’t be more items in an in-memory collection than there are memory addresses on the system. The integral types do give us the first hint that Rust is a stricter world