rust-course icon indicating copy to clipboard operation
rust-course copied to clipboard

关联函数定义

Open HUGHNew opened this issue 2 years ago • 2 comments

https://github.com/sunface/rust-course/blob/e01f9c2e4595992690f00e18b60277d2e67ee401/src/basic/method.md?plain=1#L205 从 Rust-Lang Book 此处的原文来看

All functions defined within an impl block are called associated functions because they’re associated with the type named after the impl. 那么它的意思应该是 对应类型定义在 impl 块中的函数都是 该类型的 关联函数 其中带有 self 的被称为方法(即 方法也是关联函数)

评论区此处提到过

HUGHNew avatar Jun 29 '22 05:06 HUGHNew

method 和 function 存在区别吧

sunface avatar Jun 30 '22 00:06 sunface

感觉从概念上讲 method 是 第一个参数类型为 Self 的 function f.read() 像是 read(f)或者read(&f) 的语法糖

HUGHNew avatar Jul 01 '22 07:07 HUGHNew