-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_syntax.rs
60 lines (52 loc) · 1.31 KB
/
method_syntax.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// fn main() {
// println!("Hello, world!");
//}
///////////////////////////////////////////////////////////////////////////////
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 { //self:self = &self
self.width * self.height
}
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!("The area of the rectangle is {} square pixels.",rect1.area());
}
//////////////////////////////////////////////////////////////////////////////
//method with more parameters
// #[derive(Debug)]
// struct Rectangle {
// width: u32,
// height: u32,
// }
// impl Rectangle {
// fn area(&self) -> u32 {
// self.width * self.height
// }
// fn can_hold(&self, other: &Rectangle) -> bool {
// self.width > other.width && self.height > other.height
// }
// }
// fn main() {
// let rect1 = Rectangle {
// width: 30,
// height: 50,
// };
// let rect2 = Rectangle {
// width: 10,
// height: 40,
// };
// let rect3 = Rectangle {
// width: 60,
// height: 45,
// };
// println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
// println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
// }