Lab - Week7
Lab - Week7
Based on this week’s lecture we will turn our attention to safety issues in this week’s
lab by firstly looking at the match construct and how it can be used or possible error
cases
In order to apply appropriate error handling in Rust we firstly need to understand the
match control construct and how it is used for pattern matching (errors, or lack
thereof, need to be matched to some pattern/template in order to determine the type of
error encountered and the action to be taken)
Example 1:
Implement the following code and write a brief explanation of how it works:
Example 2:
The match construct is quite useful and can even be used for checking and responding
to messages sent from a channel: Please implement the following code:
Exercise 1:
Your turn! Return to the stopwatch exercise and refactor the code so as to make use
of match in the main thread. The minutes thread increments the min value every 60
seconds and sends the value to the main via a channel. The main, will either print the
newly sent value or print the existing mins value.
Exercise 2:
Simulate a factory that passes 1000 cans between 3 sections (filling, sealing and
labelling). Use channels to pass the cans between your threads and measure the
throughput and time taken. Cans are labelled based on destination. Cans destined for
the Chinese market are labelled in Chinese, whilst those for Western and local
markets are labelled in english. Use match to implement this.
----------------------------------------------------
Example 3: Unrecoverable Errors
Rerun the code using terminal and prefix the above statement. Your output should be
something similar to the below:
Rust unwinds the code, removing each function from the stack all the way back to the
main, allowing us to identify where they panic (error) arose – although in this case we
know exactly were as we deliberately triggered the call.
Exercise 3:
--------------------------------------------
As the name suggests, recoverable errors are errors we want to handle and possibly
take some form or action – think exception handling in Java.
Most operations in Rust return a special type called ‘result’. Result is an enum used
to handle errors and is structured as follows:
In the above snippet T represents the type of the value that will be returned in a
success case within the OK variant, and E represents the type of the error that will be
returned in a failure case within the Err variant.
Exercise 4:
Write a function called even_checker() which expects an integer value and uses the
Result enum to either return True or an error message “number is not even”. From
your main pass an odd number into the even_checker() function and save the result as
Num. Use match to handle the ‘error’ of not providing an odd number
Modify your code so as to add .unwrap() at the end of the call to even_checker() - line
6, remove the match block and replace it with a println! Printing out the result. Run
your code by firstly passing in an even number and then an odd number.
------------------------------------------------------
Example 4:
Instead of unwrap we use the .expect() function for provide our own error message.
Implement the following:
Exercise 6: