晋中网站开发,黄页免费领取,app store官网,wordpress 中间截取缩略图喜欢的话别忘了点赞、收藏加关注哦#xff0c;对接下来的教程有兴趣的可以关注专栏。谢谢喵#xff01;(#xff65;ω#xff65;)
5.3.1. 什么是方法(Method)
方法和函数类似#xff0c;也是用fn关键字进行声明#xff0c;方法也有名称#xff0c;也有参数#xff…喜欢的话别忘了点赞、收藏加关注哦对接下来的教程有兴趣的可以关注专栏。谢谢喵(ω)
5.3.1. 什么是方法(Method)
方法和函数类似也是用fn关键字进行声明方法也有名称也有参数也有返回值。但方法和函数也有不同之处
方法在struct(或枚举或trait对象)的上下文中定义方法的第一个参数总是self表示方法所在的被调用的struct实例类似于Python中的self和JS中的this。
5.3.2. 方法的实际应用
接下来还是看例子以上一篇文章的代码为例
struct Rectangle { width: u32, length: u32,
} fn main() { let rectangle Rectangle{ width: 30, length: 50, }; println!({}, area(rectangle));
} fn area(dim:Rectangle) - u32 { dim.width * dim.length
}area这个函数的作用是计算面积但它很特别它只适用于矩形而不适用于其他形状或者是其他的类型。如果后面代码中要加上计算其他图形的面积的函数那么area这个名字就要混淆。如果改名成ractangle_area的话又太麻烦main函数里所有调用了这个函数的地方都要改。
所以如果能把存储矩形长款的Rectangle结构体和只能计算矩形面积area这个函数结合到一起就是最好的。
对于这种需求Rust提供了implementation(中文意为实现)其关键字是impl后边跟着struct名加上{}在里面像定义普通函数一样定义方法就行。
对于这个例子struct名就是Rectangle,把定义area函数的代码剪贴到{}内即可。
impl Rectangle { fn area(dim:Rectangle) - u32 { dim.width * dim.length }
}但注意这里的代码还不是方法因为方法的第一个参数必须是self现在的代码叫关联函数下文会讲。
这么写是没有问题的但还可以进一步简化。上文中说到了方法的第一个参数总是self,所以这里也可以改一下
impl Rectangle { fn area(self) - u32 { self.width * self.length }
}你当前写的这个方法绑定在谁上self指的就是谁这个代码中area这个函数被绑定在Rectangle上所以self就指的是Rectanglearea的参数不用拿走所有权所以在self前面加上表示印引用。
当然这么改之后,main函数里的函数调用也会改从函数的调用改到方法的调用——实例.方法名(参数):
fn main() { let rectangle Rectangle{ width: 30, length: 50, }; println!({}, rectangle.area());
}rectangle.area()的括号中不写东西是因为area方法在定义时只使用了self作为参数表示这个方法借用了self即rectangle实例的不可变引用。在调用area时你不需要显式地传递这个实例因为方法调用已经隐式地知道self是rectangle。
整体代码如下:
struct Rectangle { width: u32, length: u32,
} impl Rectangle { fn area(self) - u32 { self.width * self.length }
} fn main() { let rectangle Rectangle{ width: 30, length: 50, }; println!({}, rectangle.area());
}输出:
15005.3.3. 如何定义方法
在上面的实际应用中已经写过一遍了所以这里就只做总结
在impl里定义方法方法的第一个参数可以是self、self或是mut self。可以是获得所有权、引用或可变引用这点和其他参数一样。方法可以帮助更好的组织代码因为可以把某个类型的方法都放在impl块里面避免在整个代码库里搜索struct它相关的行为了。
5.3.4. 方法调用的运算符
在C/C中调用方法有两种运算符
-其格式为object-something()调用指针指向的对象上的方法就使用这一种也就是object为指针时.其格式为object.something()调用对象上的方法就使用这种也就是object不为指针是个对象时
而object-something()实际上是语法糖它等同于(*object).something()*表示解引用。两者的流程都是先解引用得到对象再在对象上调用方法。
Rust提供了自动引用/解引用的特性。也就是说在调用方法时Rust根据情况自动添加、mut或*以便object可以匹配方法的签名。这点和Go语言一样。
举个例子下面这两行代码效果相同
point1.distance(point2);
(point1).distance(point2);Rust会根据情况自动在point1前加上。
5.3.5. 方法的参数
方法除了self也可以带其他参数一个或多个都可以。
举个例子在5.3.2的代码基础上加一个判断矩形是否能容纳下另一个长方形的功能不考虑斜着放也不考虑矩形的长比宽长的情况
impl Rectangle { fn can_hold(self, other: Rectangle) - bool { self.width other.width self.length other.length }
}逻辑非常好想只要矩形的长和宽都比另一个大就行。
然后再在main函数里写几个Rectangle的实例输出比较结果看看有没有问题就行以下是完整代码
struct Rectangle { width: u32, length: u32,
} impl Rectangle { fn can_hold(self, other: Rectangle) - bool { self.width other.width self.length other.length }
} fn main() { let rect1 Rectangle{ width: 30, length: 50, }; let rect2 Rectangle{ width: 10, length: 40, }; println!({}, rect1.can_hold(rect2));
}输出
true5.3.6. 关联函数
可以在impl块里定义不把self作为第一个参数的函数叫关联函数不是方法。它不是在实例上调用的但它与这个类型有关联。例如: String::from()就是String这个类型上叫做from的关联函数。
关联函数通常用于构造器也就是用来被创建关联类型的一个实例。
比如说在5.3.2的代码基础上加一个构建正方形的构造器正方形也是特殊的矩形:
impl Rectangle { fn square(size: u32) - Rectangle { Rectangle{ width: size, length: size, } }
}参数只需要一个因为构造正方形只需要一个边长。
在main函数里调用一下这个关联函数试试其格式为类型名::函数名(参数)以下是完整代码
#[derive(Debug)]
struct Rectangle { width: u32, length: u32,
} impl Rectangle { fn square(size: u32) - Rectangle { Rectangle{ width: size, length: size, } }
} fn main() { let square Rectangle::square(10); println!({:?}, square);
}输出
Rectangle { width: 10, length: 10 }::不仅可以用于关联函数也可以用于模块创建命名空间以后会讲
5.3.7. 多个impl块
每个struct允许拥有多个impl块。
比如我要把这篇文章里写过的所有的方法和关联函数都写到代码里。 可以这么写多个impl块
#[derive(Debug)]
struct Rectangle { width: u32, length: u32,
} impl Rectangle { fn area(self) - u32 { self.width * self.length }
} impl Rectangle { fn can_hold(self, other: Rectangle) - bool { self.width other.width self.length other.length }
} impl Rectangle { fn square(size: u32) - Rectangle { Rectangle{ width: size, length: size, } }
} fn main() { let square Rectangle::square(10); println!({:?}, square);
}也可以这么写合在一个impl块里
#[derive(Debug)]
struct Rectangle { width: u32, length: u32,
} impl Rectangle { fn area(self) - u32 { self.width * self.length } fn can_hold(self, other: Rectangle) - bool { self.width other.width self.length other.length } fn square(size: u32) - Rectangle { Rectangle{ width: size, length: size, } }
} fn main() { let square Rectangle::square(10); println!({:?}, square);
}