-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow_control.rs
More file actions
84 lines (70 loc) · 2.12 KB
/
flow_control.rs
File metadata and controls
84 lines (70 loc) · 2.12 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
/* ----------------------------------
* ------- EXECUTE -------
* ----------------------------------
*/
pub fn run() {
match_arms();
conditionals();
loops();
}
// ############################
// ### FLOW CONTROL ###
// ############################
fn match_arms() { // (≡java switch-case)
for i in [-275, -273, -50, 0, 5, 12, 20, 28, 38] {
temp_description(i);
}
fn temp_description(t: i16) {
match t {
i16::MIN..-273 => println!("Sub -273.15°C temp invalid"),
-273..=0 => println!("Freezing"),
1..12 => println!("Chilly"),
12..20 => println!("Temperate"),
20..=28 => println!("Warm"),
// _ => println!("Hot, unsafe temperatures"), // _ = default/catch-all
x => println!("Hot, unsafe temperature: {x}°C") // x = named default/catch-all
}
}
}
fn conditionals() {
let battery: u8 = 42;
if battery > 42 {
// Battery high
} else if battery > 16 {
// Battery low
} else {
// Battery critical
}
let count: u8 = 16;
let label: &'static str = if count == 1 { "donut" } else { "donuts" }; // if-else blocks return val in Rust
println!("Inventory: {} {}", count, label);
}
fn loops() {
let numbers = [42, 2, 20, 4, 5];
for num in numbers {
print!("{} ", num)
}
let sentence = "The quick brown fox.";
let mut itr = sentence.split(' ').peekable();
while let Some(token) = itr.next() {
print!("{} ", token);
if itr.peek().is_none() {
println!();
}
}
let grid = vec![
vec![0, 0, 0, 0, 0],
vec![0, 0, 0, 1, 0],
vec![1, 0, 0, 0, 0],
];
'outer: for row in 0..grid.len() { // Loop labeling
for col in 0..grid[row].len() {
if grid[row][col] == 1 {
println!("Found a 1 at ({}, {})", row, col);
break 'outer;
}
}
}
// 'loop' can return a value w/ break
// See user_input.rs for infinite 'loop' example
}