In today's studio you will explore traits and generics and how they support different kinds of polymorphism 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., rusttraitsgenerics).
Change into the src directory within that package and edit the
main.rs file that it contains.
Above the main function, declare a
struct type named Car that contains a
String (for the car's name), and two isize
variables (for its current position).
In between the struct declaration and the main function, add an
impl block for the stuct type with an associated new
function that takes no arguments and returns an instance of the struct
type that is initialized with a value of your choice for its name (I used
"Lightning"), and 0 for each of its other fields.
In the main function declare a variable of the struct type and modify the code that prints out the "hello world!" message so that it instead prints out the fields of the variable. Compile and run the code and as the answer to this exercise please show the code you wrote and the output the program produced.
At the top of the main.rs file add the line
use std::fmt;
and immediately below that declare an
enum for the four cardinal directions: North,
East, South, and West. Add a field
of the enum type to the struct type (for the direction it is heading) and
initialize that field to North in the struct type's associated
new function.
Below that declare an impl of the fmt::Display
trait for the enum type with a single method with the signature
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
that matches on each element of the enum and writes a corresponding string literal, in a manner similar to the enum-to-string conversion example shown at https://kerkour.com/rust-enum-to-string.
Modify the main function so that it uses to_string to print out
that new field along with the other fields of the variable. Compile and run the
code and as the answer to this exercise please show the code you wrote and
the output the program produced.
Above the struct declaration, declare a trait named Mover with a
single method: fn advance(&mut self);
Below the struct declaration add an impl of the
Mover trait for the struct type: if the direction is
North the advance method should increase the
y position by 1; if
South it should decrease the y position by 1; if
East it should increase the x position by 1; or if
West it should decrease the x position by 1.
In the main function, change the declaration of the variable so that it
is mutable, and then after the first statement that prints out its fields
call the advance method on it and then again print out its fields.
Compile and run your program and as the answer to this exercise please show
the output the program produced.
Above the struct declaration, declare an enum named Rotation
with variants Left and Right and then below that
declare a trait named Turner with a
single method: fn turn(&mut self, r: Rotation);
Below the struct declaration add an impl of the
Turner trait for the struct type that matches on a tuple
consisting of the heading field and the passed Rotation value,
and for each possible combination changes the heading field to the appropriate
new Direction (for example if the heading is North,
then rotating Left would change the heading West but
rotating Right would change the heading East).
Note that this may require you to add the attribute
#[derive(Copy,Clone)] to the declaration of the enum for
Direction.
Modify the main function so that it repeatedly makes different combinations
of calls to the variable's advance and turn methods,
and prints out the variable's fields at appropriate points.
Compile and run your program and as the answer to this exercise please show
the output the program produced.
Just above your program's main function, declare and define a generic function with signature:
fn figure_eight<T: Mover + Turner + std::fmt::Debug>
(c: &mut T)
that causes the variable that is aliased by the passed mutable reference to perform a "figure eight" maneuver as follows: (1) advances then prints out the variable using "{:?}", (2) turns left then advances then prints out the variable using "{:?}", (3) turns left then advances then prints out the variable using "{:?}", (4) turns left then advances then prints out the variable using "{:?}", (5) advances then prints out the variable using "{:?}", (6) turns right then advances then prints out the variable using "{:?}", (7) turns right then advances then prints out the variable using "{:?}", (8) turns right then advances then prints out the variable using "{:?}".
Use the #[cfg(oldexercise)] attribute to compile out all
of the statements below the variable declaration in the main function,
and instead have the main function pass a mutable reference to the
variable into a call the generic function.
Note that this may require additional uses of the
#[derive] attribute to provide derived implementations of
different traits for different types your code is using.
Compile and run your program and as the answer to this exercise please show
the code for the generic function and the output the program produced.
Change into the top level directory for this studio, and 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 17:45 Monday August 18th, 2025, by Chris Gill.