知识卡片
拼接方式决定所有权
push_str 把一个 &str 追加到已有可变 String,push 追加一个 char。s1 + &s2 会把 s1 移给 add,借用并追加 s2 的内容,然后交回结果。&String 可以强制转换为 &str。多段组合可用 format! 接受借用并生成新文本,保有原来各个变量。
相关卡片 [[字符串包装有效UTF8]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 常用集合 Storing UTF-8 Encoded Text with Strings · 查看原书依据
Second, we can see in the signature that add takes ownership of self because self does not have an &. This means s1 in Listing 8-18 will be moved into the add call and will no longer be valid after that.
参考来源
- 位置:《The Rust Programming Language(第2版)》第08章 常用集合(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c08.xhtml`,块 0112)
- 结论依据:原文从 add 的 self 签名推导左侧被移动、右侧被借用的结果。
- 原始内容:Second, we can see in the signature that add takes ownership of self because self does not have an &. This means s1 in Listing 8-18 will be moved into the add call and will no longer be valid after that.