知识卡片
点调用会调整接收者
调用 player.location() 时,Rust 可以按方法要求解引用 player,或自动为接收者创建共享或可变借用。这样 Player、&Player 或 Box 可使用同样的调用写法,具体方法仍需具备所需的移动或借用权限。普通函数参数依旧按自身的类型和自动转换规则检查。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Expressions 调用点自动调整接收者与标注泛型 · 查看原书依据
Rust’s . operator automatically dereferences player or borrows a reference to it as needed.
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第05章《Expressions》|调用点自动调整接收者与标注泛型,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch05.xhtml#function-and-method-calls`
- 结论依据:原文对比把 &i32 传给期待 i32 的函数会报错,以及点调用寻找合适的接收者形式。
- 原始内容:Rust’s . operator automatically dereferences player or borrows a reference to it as needed.