In today's studio you will explore enums and patterns in Rust.
Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.
As you work through these exercises, please record your answers, and when you finish them please log into Canvas, select this course in this semester, and then on the Canvas page for this studio assignment upload (1) a file containing your answers and (2) any code you produced while answering the exercises. Only one submission per team, please, and if you need to re-submit it the person who originally submitted the studio should please be the one to do that.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
As the answer to the first exercise, list the names of the people who worked together on this studio.
Log in using ssh into shell.cec.wustl.edu
using your WUSTL
Key id and password, issue the qlogin
command to
get onto one of the Linux Lab machines, and then within the directory you
created for this course, add a new directory for this studio.
In that new directory, use the cargo new
command to create a new
package (named e.g., rustenumspatterns
).
Change into the src
directory within that package and edit the
main.rs
file that it contains. Replace the contents of the main
function with a declaration of an immutable variable that is initialized to an
unsigned integer value of your choosing. Below that declaration, add a
match
expression for that variable with a single arm whose
pattern is the value that was used to initialize the variable and prints out
a message saying that the value was matched. Compile your program and as the
answer to this exercise please show the error that occurred.
Add another arm to the match expression, with the wildcard pattern
_
that prints out another message indicating that arm matched.
Compile and run your code (1) with the new arm listed before the other one
and then (2) with the new arm listed after the other one. As the answer to
this exercise please explain which arm was matched in each case.
Change the variable declaration so that it is initialized with a tuple consisting of a string literal and an unsigned integer value. Remove the non-wildcard arms of the match expresion and add an arm before the wildcard arm, with a pattern that is specifically the tuple that was used to initialize the variable and prints out a message indicating that match. Compile and run your program and as the answer to this exercise please show the output the program produced.
Then, change the value in one of the fields that is used to initialize the variable. Compile and run your program and as the answer to this exercise please show the output the program produced and explain briefly why a different output than in the previous exercise was produced.
Replace both arms of the match expression with a single arm whose pattern
is a tuple with fields named name
and number
respectively, and uses name
and number
to print
out the tuple that was matched. Compile and run your program and as the
answer to this exercise please show the output the program produced and
explain briefly why having a single branch didn't produce an error or warning
like the case in the previous exercise where having a single branch did.
Put the code inside the main function's body into a conditional compilation block, e.g.,
#[cfg(oldexercise)] { let ... }
, to preserve it.
Below the conditional computation block, add the statement
use std::str::FromStr;
and after that declare a variable that
is initialized with a string literal, and match on the result of a call to
u8::from_str
with an immutable reference to that variable.
The first arm should match on the case where the variable represented
a valid 8 bit unsigned integer value and should print out a message saying
that it is; the second arm should match on the case where it did not and should
print out a message saying that it did not. Compile and run your program with
the variable initialized to "hello"
and then again with the
variable initialized to "31"
and as the answer to this exercise
please show the output the program produced in each case.
Make a copy of the match expression you added in the previous exercise and put the original expression into a conditional compilation block, e.g.,
#[cfg(oldexercise)] { match ... }
, to preserve it.
Paste the copy of the match
expression below that (still inside
the main function), and replace it with an equivalent
if let
expression. Test your code to make sure the new
expression correctly distinguishes string literals that represent valid
8 bit unsigned integer values from those that do not, and as the answer to
this exercise please show the code for that if let
expression.
For this studio, please turn in the following:
Page posted Sunday August 25, 2024, and updated Friday August 30, 2024, by Chris Gill.