0% found this document useful (0 votes)
8 views5 pages

Lab - Week7

The document discusses using Rust's match construct for error handling. It provides examples of using match to check for errors from channels. Exercises demonstrate using match to refactor code to handle errors and simulate a factory passing items between threads.

Uploaded by

potatoes.clowns
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Lab - Week7

The document discusses using Rust's match construct for error handling. It provides examples of using match to check for errors from channels. Exercises demonstrate using match to refactor code to handle errors and simulate a factory passing items between threads.

Uploaded by

potatoes.clowns
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Realtime Systems Lab – 8

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

Implement the following code:


In the above example we are using the panic! macro to deliberately kill the program.
Rust provides panic! as mechanism through which to terminate a program in
unrecoverable (ie errors that cannot be addressed) situation. The program will
terminate by printing the string passed into panic! and advise the user to rerun the
program using RUST_BACKTRACE=1.

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:

Modify the stopwatch program so as to trigger a panic at 1.57.

--------------------------------------------

Example 3: Recoverable Errors

As the name suggests, recoverable errors are errors we want to handle and possibly
take some form or action – think exception handling in Java.

Please implement the following code:

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

Your code should be structured as follows:


Exercise 5:

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.

Explain what happens

------------------------------------------------------
Example 4:

Instead of unwrap we use the .expect() function for provide our own error message.
Implement the following:

Exercise 6:

Return to the bakery exercise and make the following modifications:


 There is a 1 in 10 probability that a bun has failed to rise – ie its spoilt. Add a
variable to your bun struct to indicate whether it is OK or not.
 Using appropriate error handling buns should only be placed on the rack if
they are not spoilt.

You might also like