-
Notifications
You must be signed in to change notification settings - Fork 33
/
tutorial-05_2-enum.rs
79 lines (70 loc) · 1.88 KB
/
tutorial-05_2-enum.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
67
68
69
70
71
72
73
74
75
76
77
78
#![allow(dead_code)]
/**
* 5.2 Enums
* http://doc.rust-lang.org/tutorial.html#enums
* http://doc.rust-lang.org/rust.html#compiler-features
*
* @license MIT license <http://www.opensource.org/licenses/mit-license.php>
*/
use std::f64::consts::PI;
#[derive(Clone, Copy)]
struct Point {
x: f64,
y: f64
}
/*
* Enum variant
*/
enum Shape {
Circle(Point, f64),
Rectangle(Point, Point)
}
fn area(sh: Shape) -> f64 {
match sh {
Shape::Circle(_, size) => PI * size * size,
Shape::Rectangle(Point { x, y }, Point { x: x2, y: y2 }) => (x2 - x) * (y - y2)
}
}
/*
* Enum of structs
*/
enum StructShape {
StructCircle { center: Point, radius: f64 },
StructRectangle { top_left: Point, bottom_right: Point }
}
/**
* Compute area of a Shape
*/
fn struct_area(sh: StructShape) -> f64 {
match sh {
StructShape::StructCircle { radius, .. } => PI * radius * radius,
StructShape::StructRectangle { top_left, bottom_right } => {
(bottom_right.x - top_left.x) * (top_left.y - bottom_right.y)
}
}
}
/**
* Main program
*/
fn main() {
let top_left = Point { x: 0.0, y: 1.0 };
let bottom_right = Point { x: 1.0, y: 0.0 };
/*
* Enum variant
*/
let my_circle1 = Shape::Circle(top_left, 1.0);
let my_rectangle1 = Shape::Rectangle(top_left, bottom_right);
let circle_area1: f64 = area(my_circle1);
let rectangle_area1: f64 = area(my_rectangle1);
println!("variant: circle area: {}", circle_area1);
println!("variant: rectangle area: {}", rectangle_area1);
/*
* Enum of structs
*/
let my_circle = StructShape::StructCircle { center: top_left, radius: 1.0};
let my_rectangle = StructShape::StructRectangle { top_left: top_left, bottom_right: bottom_right};
let circle_area: f64 = struct_area(my_circle);
let rectangle_area: f64 = struct_area(my_rectangle);
println!("struct: circle area: {}", circle_area);
println!("struct: rectangle area: {}", rectangle_area);
}