小例子:main.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
mod test_mod {
pub struct TestStruct {
num: u32,
}

impl TestStruct {
pub fn new() -> TestStruct {
TestStruct { num: 0}
}
pub fn get_num(&self) -> u32 { self.num }
pub fn set_num(&mut self, value: u32) { self.num = value}
}
}

use test_mod::TestStruct;

fn main() {
let mut i = TestStruct::new();
println!("i: {}", i.get_num());
i.set_num(30);
println!("i: {}", i.get_num());
}