← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 10649.b · 生成模型:未记录

发送消息移动所有权

理解等级:中级

send 以值接收参数。发送 String 后,发送者不能再从旧变量读取它,接收者独立接管这个值。编译器通过所有权约束排除了另一个线程已修改或释放数据、发送者仍访问旧值的情形。需要留本地副本时,应在发送前按实际需求决定是否复制或克隆。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第16章 无畏并发(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c16.xhtml`,块 0104) - 结论依据:原文把发送后使用 val 的编译错误归因于 send 获取所有权,消息随后由接收方持有。 - 原始内容:Our concurrency mistake has caused a compile-time error. The send function takes ownership of its parameter, and when the value is moved the receiver takes ownership of it. This stops us from accidentally using the value again after sending it; the ownership system checks that everything is okay.