知识卡片
范围索引通过trait取得切片
&vector[1..3]用含下界、不含上界的范围选取元素1与2。范围按SliceIndex<[T]>提供切片索引能力,Vec的Index实现返回对应引用,表达式再保留借用。范围必须有效,不能假定越界时自动截断;是否使用可失败访问接口应按调用场景选择。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Slices · 查看原书依据
Range<usize> type, which holds an inclusive lower bound and an exclusive upper bound. The Range type implements the SliceIndex<T> trait, which describes indexing operations on slices of an arbitrary type T (so the Output type is [T]). The vector[ ] part is an indexing expression; the compiler converts this into an invocation of the Index trait’s index method
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Slices》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u043-ch01-058)。
- 结论依据:原文在“Range<usize>”附近的定义、例子或代码支撑本卡核心说明:&vector[1..3]用含下界、不含上界的范围选取元素1与2。边界按已读前提解释;示例未执行。
- 原始内容:Range<usize> type, which holds an inclusive lower bound and an exclusive upper bound. The Range type implements the SliceIndex<T> trait, which describes indexing operations on slices of an arbitrary type T (so the Output type is [T]). The vector[ ] part is an indexing expression; the compiler converts this into an invocation of the Index trait’s index method