Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

50.3.1Automaton State

We define the automaton states using an enumerated type:

We use if/else statements to define the next state:

We use a loop on call 10 times next_state():

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:

The area() function accepts a Shape object as argument and uses a match expression to compute the area for each shape:

Here are the results on each shape:

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:

We call the new_price() function on different values:

[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:

We use a match statement on self to process each animal:

We loop on all animals and call the get_nb_feet() method to get the number of feet: