In today's studio you will explore how different utility traits work in Rust, and how they can be used to modify and characterize program behavior.
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., utilitytraits
).
Change into the src
directory within that package and in
the main.rs
file that it contains, declare a struct that has
a string as it's only field, and implement an associated new
function that takes a reference to a string and uses it to initialize
the struct. Modify the main
function
so that it declares two variables of the struct type, initialized with two
different strings, and prints out the structs in the order they were declared.
Compile and run your program, and as the answer to this exercise please show the code you wrote and the output the program produced.
Implement the Drop
trait for the struct, and in the drop
method of that implementation print out the value of the struct with a message
saying it is being dropped.
Compile and run your program, and as the answer to this exercise please (1) show
your code that implements the Drop
trait, (2) show the output the
program produced, and (3) explain how the order in which the structs were dropped
relates to the order in which they were declared.
Modify your struct and the implementation blocks for it (and as necessary the main function as well) so that the type of the struct's field is a generic parameter. Compile and run your program, and confirm that it produces similar (or the same) output as before. As the answer to this exercise please (1) show your code and (2) explain what changes (if any) you had to make in addition to adding the generic parameters, in order to make this work.
Modify your code so that the struct has two member fields of the parameterized
type, and its new
function takes two values that it uses to intialize
the struct's fields. Modify your program's main
function so that (1) the
first struct variable is parameterized with String
and the second
struct variable is parameterized with u16
, (2) each struct is
initialized using two different values, and (3) the values of each struct are printed
out.
Compile and run your program, and as the answer to this exercise please show the output it produced.
Put the code that implements the Drop
trait into a conditional
compilation block, e.g., using #[cfg(oldexercise)]
, to preserve it.
Then, use the #[derive]
attribute to derive an implemementation of
the Default
trait for the struct type.
In your main function declare two more struct variables of the same types as
the previous ones, using Default::default()
to initialize one of
the fields of each struct and giving a specific value to initialize the other field.
Compile and run your program, and as the answer to this exercise please show the output the program produced.
Remove the conditional compilation block from the code that implements the
Drop
trait. Compile and run your program, and as the answer to this
exercise please say whether an error occurred and if so why that happened.
For this studio, please turn in the following:
Page posted Wednesday October 16, 2024, by Chris Gill.