-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_car.rs
69 lines (58 loc) · 1.64 KB
/
exercise_car.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
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,
}
fn car_quality(miles: u32) -> (Age, u32) {
let quality: (Age, u32) = (Age::New, miles);
quality
}
fn car_factory(color: String, motor: Transmission, roof: bool, miles: u32) -> Car {
let age = car_quality(miles);
Car {
color,
motor,
roof,
age,
}
}
fn main() {
// Create car color array
let colors = ["Blue", "Green", "Red", "Silver"];
// Declare the car type and initial values
let mut car: Car;
let mut engine = Transmission::Manual;
// Order 3 cars, one car for each type of transmission
// Car order #1: New, Manual, Hard top
car = car_factory(String::from(colors[0]), engine, true, 0);
println!(
"Car order 1: {:?}, Hard top = {}, {:?}, {}, {} miles",
car.age.0, car.roof, car.motor, car.color, car.age.1
);
// Car order #2: New, Semi-automatic, Convertible
engine = Transmission::SemiAuto;
car = car_factory(String::from(colors[1]), engine, false, 100);
println!(
"Car order 2: {:?}, Hard top = {}, {:?}, {}, {} miles",
car.age.0, car.roof, car.motor, car.color, car.age.1
);
// Car order #3: New, Automatic, Hard top
engine = Transmission::Automatic;
car = car_factory(String::from(colors[2]), engine, true, 200);
println!(
"Car order 3: {:?}, Hard top = {}, {:?}, {}, {} miles",
car.age.0, car.roof, car.motor, car.color, car.age.1
);
}