-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
66 lines (55 loc) · 1.17 KB
/
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
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
61
62
63
64
65
66
struct Point {
x: f64,
y: f64,
}
impl Point {
fn origin() -> Point {
Point { x: 0.0, y: 0.0 }
}
fn new(x: f64, y: f64) -> Point {
Point { x, y }
}
}
struct Rectangle {
p1: Point,
p2: Point,
}
impl Rectangle {
fn area(&self) -> f64 {
let Point { x: x1, y: y1 } = self.p1;
let Point { x: x2, y: y2 } = self.p2;
((x2 - x1) * (y2 - y1)).abs()
}
fn perimeter(&self) -> f64 {
let Point { x: x1, y: y1 } = self.p1;
let Point { x: x2, y: y2 } = self.p2;
2.0 * ((x1 - x2).abs() + (y1 - y2).abs())
}
fn translate(&mut self, x: f64, y: f64) {
self.p1.x += x;
self.p2.x += x;
self.p1.y += y;
self.p2.y += y;
}
}
struct Pair(Box<i32>, Box<i32>);
impl Pair {
fn destroy(self) {
let Pair(first, second) = self;
println!("Destroy pair({},{})", first, second);
}
}
fn main() {
let rectangle = Rectangle {
p1: Point::origin(),
p2: Point::new(3.0, 4.0),
};
println!("Rectangle perimeter: {}", rectangle.perimeter());
let mut square = Rectangle {
p1: Point::origin(),
p2: Point::new(1.0, 2.0),
};
square.translate(2.0, 2.0);
let pair = Pair(Box::new(1), Box::new(2));
pair.destroy();
}