-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14-impl.rs
More file actions
29 lines (24 loc) · 847 Bytes
/
14-impl.rs
File metadata and controls
29 lines (24 loc) · 847 Bytes
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
struct Rectangle {
length: u32,
width: u32,
}
impl Rectangle {
fn area(&self) -> u32{//method //方法
self.length * self.width
}
fn can_hold(&self, other: &Rectangle) -> bool{
self.width > other.width && self.length > other.length
}
fn square(size: u32) -> Rectangle{//associated functions //关联函数
Rectangle{ length: size, width: size};
}
}
fn main(){
let rect1 = Rectangle{ length: 10, width: 20};
let rect2 = Rectangle{ length: 20, width: 30};
let rect3 = Rectangle::square(4);//use associated functions to build a 4X4 square
println!("The area of the rect1 is {} square pixels.",
rect1.area() );
println!("The rect1 can hold rect2?: {}", rect1.can_hold(&rect2));
println!("The rect2 can hold rect1?: {}", rect2.can_hold(&rect1));
}