知识卡片
数组长度进入类型
[T; N] 表示固定个数的同类型元素,例如月份表。动态索引达到或超过长度时,方括号访问会触发 panic,避免继续读写无效地址。外部下标需要可恢复行为时,可先检查边界或使用返回 Option 的查询。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 常见编程概念 Compound Types · 查看原书依据
You write an array’s type using square brackets with the type of each element, a semicolon, and then the number of elements in the array, like so:
参考来源
- 位置:《The Rust Programming Language(第2版)》第03章 常见编程概念(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c03.xhtml`,块 0133)
- 结论依据:书中用 [i32; 5] 把元素类型与个数一起写在类型中。
- 原始内容:You write an array’s type using square brackets with the type of each element, a semicolon, and then the number of elements in the array, like so: