知识卡片

普通读书笔记卡 · 6074.e

接收者表达访问契约

理解等级:中级

方法接收者表达调用方交出的访问能力:&self为共享借用,&mut self为独占可变借用,self按值取得接收者。共享借用仍可能通过内部可变性机制改变状态,按值参数也可能复制Copy值;不能把这三种签名简单等同于绝不修改、一定修改和一定销毁。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 1. Types Functions and Methods · 查看原书依据

      &self) -> f64 { match self { Shape::Rectangle { width, height } => width * height, Shape::Circle { radius } => std::f64::consts::PI * radius * radius, } } } The name of a method creates a label for the behavior it encodes, and the method signature gives type information for its inputs and outputs. The first input for a method will be some variant of self, in

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Functions and Methods》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u016-ch01-013)。 - 结论依据:原文在“&self”附近的定义、例子或代码支撑本卡核心说明:方法接收者表达调用方交出的访问能力:&self为共享借用,&mut self为独占可变借用,self按值取得接收者。边界按已读前提解释;示例未执行。 - 原始内容:&self) -> f64 { match self { Shape::Rectangle { width, height } => width * height, Shape::Circle { radius } => std::f64::consts::PI * radius * radius, } } } The name of a method creates a label for the behavior it encodes, and the method signature gives type information for its inputs and outputs. The first input for a method will be some variant of self, in