知识卡片
用切片统一借用连续数据
&[T] 是对连续序列的借用,携带首元素地址和元素数。函数参数若写 &[f64],就可以用同一实现读取全数组、整向量或一段数据,如 &v[0..2]。借用时无需复制元素;范围超出原数据会 panic。只读算法可接收共享切片,排序等需修改元素的算法用 &mut [T]。
关联阅读:[[连续序列的索引边界]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Fundamental Types 切片让算法跨容器复用 · 查看原书依据
A reference to a slice is a fat pointer: a two-word value comprising a pointer to the slice’s first element, and the number of elements
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第02章《Fundamental Types》|切片让算法跨容器复用,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch02.xhtml#slices`
- 结论依据:原文用切片的两字表示、三个切片表达式,以及能接收两类容器的打印函数,说明为何切片适合作参数。
- 原始内容:A reference to a slice is a fat pointer: a two-word value comprising a pointer to the slice’s first element, and the number of elements