50.3.1Automaton State¶
We define the automaton states using an enumerated type:
#[derive(Debug, PartialEq)]
enum State {
Start,
Stage1,
Stage2,
Stage3,
Stop,
}We use if/else statements to define the next state:
fn next_state(s: State) -> State {
if s == State::Start {
State::Stage1
} else if s == State::Stage1 {
State::Stage2
} else if s == State::Stage2 {
State::Stage3
} else if s == State::Stage3 {
State::Stop
} else {
State::Start
}
}We use a loop on call 10 times next_state():
let mut s = State::Stop;
for i in 0..10 {
s = next_state(s);
println!("Step {i}: state = {s:?}");
}Step 0: state = Start
Step 1: state = Stage1
Step 2: state = Stage2
Step 3: state = Stage3
Step 4: state = Stop
Step 5: state = Start
Step 6: state = Stage1
Step 7: state = Stage2
Step 8: state = Stage3
Step 9: state = Stop
()50.3.2Shapes area¶
We define an enum type with four variants accepting the needed
parameters for computing the area:
enum Shape {
Disk(f32), // Radius
Square(f32), // Side length
Rectangle(f32, f32), // Side lengths
Triangle(f32, f32), // Base length and Height
}The area() function accepts a Shape object as argument and uses a
match expression to compute the area for each shape:
fn area(s: Shape) -> f32 {
match s {
Shape::Disk(radius) => radius * radius * std::f32::consts::PI,
Shape::Square(a) => a * a,
Shape::Rectangle(a, b) => a * b,
Shape::Triangle(b, h) => b * h / 2.0,
}
}Here are the results on each shape:
println!("Disk: {}", area(Shape::Disk(3.0)));
println!("Square: {}", area(Shape::Square(2.0)));
println!("Rectangle: {}", area(Shape::Rectangle(2.0, 3.0)));
println!("Triangle: {}", area(Shape::Triangle(2.0, 3.0)));Disk: 28.274334
Square: 4
Rectangle: 6
Triangle: 3
50.3.3Nuts price¶
The function new_price() uses ranges inside the match statement to
handle the different cases:
fn new_price(m: f32, x: f32) -> f32 {
match m {
..1.0 => 1.2 * x,
1.0..2.0 => x,
2.0..4.0 => 0.8 * x,
_ => 0.5 * x,
}
}We call the new_price() function on different values:
let mass_prices = [(0.3, 3.4), (1.2, 2.7), (2.0, 2.9), (4.0, 3.2)];
for (m, x) in mass_prices {
print!("{}, ", new_price(m, x));
}[E0308] Error: mismatched types
╭─[ command_12:1:1 ]
│
2 │ for (m, x) in mass_prices {
│ ─────┬─────
│ ╰─────── here the type of `mass_prices` is inferred to be `[(f32, f32); 4]`
───╯50.3.4Animal feet¶
The Animal enumerated type is defined as following:
#[derive(Debug)]
enum Animal {
Cat,
Dog,
Snake,
Crow,
}We use a match statement on self to process each animal:
impl Animal {
fn get_nb_feet(&self) -> u8 {
match self {
Animal::Cat | Animal::Dog => 4,
Animal::Snake => 0,
Animal::Crow => 2,
}
}
}We loop on all animals and call the get_nb_feet() method to get the
number of feet:
for animal in [Animal::Cat, Animal::Dog, Animal::Snake, Animal::Crow] {
println!("The {animal:?} has {} feet.", animal.get_nb_feet());
}