03-使用方法进行封装
使用方法进行封装 — Rust 语言之旅
内容来源 · Tour of Rust / Rust 语言之旅
原文链接: https://tourofrust.com/76_zh-cn.html
使用方法进行封装
Rust 支持对象的概念。“对象”是一个与一些函数(也称为方法)相关联的结构体。
任何方法的第一个参数必须是与方法调用相关联的实例的引用。(例如 instanceOfObj.foo())。Rust 使用:
&self —— 对实例的不可变引用。&mut self —— 对实例的可变引用。
方法是在一个有 impl 关键字的实现块中定义的:
1
2
3
4
5
6
| impl MyStruct {
...
fn foo(&self) {
...
}
}
|
示例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| struct SeaCreature {
noise: String,
}
impl SeaCreature {
fn get_sound(&self) -> &str {
&self.noise
}
}
fn main() {
let creature = SeaCreature {
noise: String::from("blub"),
};
println!("{}", creature.get_sound());
}
|