-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_types.rs
More file actions
143 lines (115 loc) · 3.43 KB
/
data_types.rs
File metadata and controls
143 lines (115 loc) · 3.43 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#![allow(dead_code, unused_variables)]
/* ----------------------------------
* ------- EXECUTE -------
* ----------------------------------
*/
pub fn run() {
enums();
structs();
}
// ######################
// ### ENUM ###
// ######################
fn enums() {
let piggy_bank = [Coin::Nickel, Coin::Penny, Coin::Quarter, Coin::Dime, Coin::Penny];
let mut savings = 0;
for coin in piggy_bank {
savings += value_in_cents(coin);
}
println!("Balance: {savings}¢");
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25
}
}
// ----------------------------------------------------
enum Operation {
Add(u8, u8),
Mul(u8, u8),
Sub { first: u8, second: u8 },
Div { dividend: u8, divisor: u8 },
}
impl Operation {
fn result(&self) -> u8 {
match self {
Self::Add(a, b) => a + b, // Self can replace enum name, e.g. Operation
Self::Sub { first, second } => first - second,
Self::Mul(a, b) => a * b,
Self::Div { dividend, divisor } => dividend / divisor,
}
}
}
let op = Operation::Sub {
first: 75,
second: 20
};
println!("Result: {}", op.result());
}
// ########################
// ### STRUCT ###
// ########################
fn structs() {
struct PkmnRegion(String, u8); // Tuple struct
let hoenn = PkmnRegion("Hoenn".to_string(), 3);
#[derive(Debug)]
enum PkmnType {
Water, Fire, Grass
}
struct Pokemon { // struct
national_dex_no: u16,
name: String,
level: u8,
weight_kg: f32,
battle_type: PkmnType
}
let mut bulbasaur = Pokemon {
national_dex_no: 0001,
name: String::from("Bulbasaur"),
level: 15,
weight_kg: 6.9,
battle_type: PkmnType::Grass
};
println!("{} - lvl. {}", bulbasaur.name, bulbasaur.level);
impl Pokemon {
fn new(name: String, dex_no: u16, weight_kg: f32, battle_type: PkmnType) -> Pokemon {
Pokemon {
national_dex_no: dex_no,
name, level: 1,
weight_kg: weight_kg,
battle_type: battle_type
}
} // Associated function (≡java static) called via double-colon, e.g. Pokemon::new()
fn get_name(&self) -> &str {
&self.name
}
fn level_up(&mut self) {
self.level += 1;
}
fn evolve(self, evolution: &str) {
println!("{} is evolving into {}!", self.name, evolution);
}
}
bulbasaur.level_up();
println!("{} - lvl. {}", bulbasaur.get_name(), bulbasaur.level);
bulbasaur.evolve("Ivysaur");
let ivysaur = Pokemon::new("Ivysaur".to_string(), 2, 13.0, PkmnType::Grass);
println!("{} is a {:?} type.", ivysaur.name, ivysaur.battle_type);
}
// #########################
// ### STRINGS ###
// #########################
// ############################
// ### CONTAINERS ###
// ############################
// ############################
// ### DATA TYPES ###
// ############################