知识卡片
切片让范围依附原数据
&[T] 和 &str 是借用的连续区间。裸下标只是数字,源内容更新后可能已经失效。返回借用切片可让编译器限制结果仍在使用时对源的独占修改。字符串的范围端点还必须在合法 UTF-8 边界。
相关卡片 [[可变借用要求独占]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 理解所有权 The Slice Type · 查看原书依据
Now when we call first_word, we get back a single value that is tied to the underlying data. The value is made up of a reference to the starting point of the slice and the number of elements in the slice.
参考来源
- 位置:《The Rust Programming Language(第2版)》第04章 理解所有权(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c04.xhtml`,块 0231)
- 结论依据:原文把返回值解释为与底层数据相连的起点和长度。
- 原始内容:Now when we call first_word, we get back a single value that is tied to the underlying data. The value is made up of a reference to the starting point of the slice and the number of elements in the slice.