知识卡片
用str切片泛化接口
只读文本接口若只需要 UTF-8 内容,通常可接收 &str。字符串字面量已经能直接使用,拥有的 String 可借出完整或部分切片。参数类型表达所需能力,调用者可自由选择持有文本的方式。
相关卡片 [[切片让范围依附原数据]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 理解所有权 The Slice Type · 查看原书依据
A more experienced Rustacean would write the signature shown in Listing 4-9 instead because it allows us to use the same function on both &String values and &str values.
参考来源
- 位置:《The Rust Programming Language(第2版)》第04章 理解所有权(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c04.xhtml`,块 0247)
- 结论依据:书中将 first_word 参数由 &String 改为 &str,展示兼容不同文本来源。
- 原始内容:A more experienced Rustacean would write the signature shown in Listing 4-9 instead because it allows us to use the same function on both &String values and &str values.