-
Notifications
You must be signed in to change notification settings - Fork 0
/
map-hash-excercise.rs
129 lines (113 loc) · 3.43 KB
/
map-hash-excercise.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
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
#[derive(PartialEq, Debug)]
struct Car {
color: String,
motor: Transmission,
roof: bool,
age: (Age, u32),
}
#[derive(PartialEq, Debug)]
enum Transmission {
Manual,
SemiAuto,
Automatic,
}
#[derive(PartialEq, Debug)]
enum Age {
New,
Used,
}
// Get the car quality by testing the value of the input argument
// - miles (u32)
// Return tuple with car age ("New" or "Used") and mileage
fn car_quality(miles: u32) -> (Age, u32) {
// Check if car has accumulated miles
// Return tuple early for Used car
if miles > 0 {
return (Age::Used, miles);
}
// Return tuple for New car, no need for "return" keyword or semicolon
(Age::New, miles)
}
// Build "Car" using input arguments
fn car_factory(order: i32, miles: u32) -> Car {
let colors = ["Blue", "Green", "Red", "Silver"];
// Prevent panic: Check color index for colors array, reset as needed
// Valid color = 1, 2, 3, or 4
// If color > 4, reduce color to valid index
let mut color = order as usize;
if color > 4 {
// color = 5 --> index 1, 6 --> 2, 7 --> 3, 8 --> 4
color = color - 4;
}
// Add variety to orders for motor type and roof type
let mut motor = Transmission::Manual;
let mut roof = true;
if order % 3 == 0 {
// 3, 6, 9
motor = Transmission::Automatic;
} else if order % 2 == 0 {
// 2, 4, 8, 10
motor = Transmission::SemiAuto;
roof = false;
} // 1, 5, 7, 11
// Return requested "Car"
Car {
color: String::from(colors[(color - 1) as usize]),
motor: motor,
roof: roof,
age: car_quality(miles),
}
}
fn main() {
// Initialize a hash map for the car orders
// - Key: Car order number, i32
// - Value: Car order details, Car struct
use std::collections::HashMap;
let mut orders: HashMap<i32, Car> = HashMap::new();
// Initialize counter variable
let mut order = 1;
// Declare a car as mutable "Car" struct
let mut car: Car;
// Order 6 cars, increment "order" for each request
// Car order #1: Used, Hard top
car = car_factory(order, 1000);
println!(
"{}: {:?}, Hard top = {}, {:?}, {}, {} miles",
order, car.age.0, car.roof, car.motor, car.color, car.age.1
);
// Car order #2: Used, Convertible
order = order + 1;
car = car_factory(order, 2000);
println!(
"{}: {:?}, Hard top = {}, {:?}, {}, {} miles",
order, car.age.0, car.roof, car.motor, car.color, car.age.1
);
// Car order #3: New, Hard top
order = order + 1;
car = car_factory(order, 0);
println!(
"{}: {:?}, Hard top = {}, {:?}, {}, {} miles",
order, car.age.0, car.roof, car.motor, car.color, car.age.1
);
// Car order #4: New, Convertible
order = order + 1;
car = car_factory(order, 0);
println!(
"{}: {:?}, Hard top = {}, {:?}, {}, {} miles",
order, car.age.0, car.roof, car.motor, car.color, car.age.1
);
// Car order #5: Used, Hard top
order = order + 1;
car = car_factory(order, 3000);
println!(
"{}: {:?}, Hard top = {}, {:?}, {}, {} miles",
order, car.age.0, car.roof, car.motor, car.color, car.age.1
);
// Car order #6: Used, Hard top
order = order + 1;
car = car_factory(order, 4000);
println!(
"{}: {:?}, Hard top = {}, {:?}, {}, {} miles",
order, car.age.0, car.roof, car.motor, car.color, car.age.1
);
}