fix: associated functions definition

All functions defined within an impl block are called associated functions because they’re associated with the type named after the impl.

> https://doc.rust-lang.org/book/ch05-03-method-syntax.html#associated-functions
pull/1551/head
L-Super 5 months ago committed by GitHub
parent e60a32f838
commit c3791dccc4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -214,7 +214,7 @@ fn main() {
现在大家可以思考一个问题,如何为一个结构体定义一个构造器方法?也就是接受几个参数,然后构造并返回该结构体的实例。其实答案在开头的代码片段中就给出了,很简单,参数中不包含 `self` 即可。
这种定义在 `impl` 中且没有 `self` 的函数被称之为**关联函数** 因为它没有 `self`,不能用 `f.read()` 的形式调用,因此它是一个函数而不是方法,它又在 `impl` 中,与结构体紧密关联,因此称为关联函数
所有在 impl 块中定义的函数被称为 **关联函数**,因为它在 `impl` 中,与结构体紧密关联
在之前的代码中,我们已经多次使用过关联函数,例如 `String::from`,用于创建一个动态字符串。

Loading…
Cancel
Save