-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructs.rs
60 lines (48 loc) · 1.09 KB
/
structs.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
/// Examples of structs and enums.
/// Bart Massey 2021
// Struct example.
#[derive(Debug)]
struct Point {
x: u64,
y: u64,
}
impl Point {
fn x_coord(&self) -> u64 {
self.x
}
}
// Tuple struct example.
struct TPoint(u64, u64);
impl TPoint {
fn x_coord(&self) -> u64 {
// or "self.0"
let TPoint(x, _) = *self;
x
}
}
// Enum example.
pub enum Color {
Red,
Green,
Blue,
Custom(f64, f64, f64),
}
impl Color {
fn red(&self) -> f64 {
match self {
Color::Red => 1.0,
Color::Green => 0.0,
Color::Blue => 0.0,
Color::Custom(r, _, _) => *r,
}
}
}
fn main() {
let p = Point { x: 0, y: 0 };
println!("{:?}", p);
println!("{}", p.x_coord());
let p = TPoint(0, 0);
println!("{}", p.x_coord());
let c = Color::Custom(0.5, 0.5, 0.0);
println!("{}", c.red());
}