In today's studio you will explore how shared and mutable references can be used in Rust, as well as some of the ways that Rust does not let them be used due to different safety issues.
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., rustreferences).
Change into the src directory within that package and edit the
main.rs file that it contains.
Modify the main function so that it declares an immutable
variable that is initialized to an integer value, declares a second
variable that is a shared (immutable) reference to the first variable,
declares a third variable that is a shared (immutable) reference to the
second variable, and then prints out the values of all three variables.
Compile and run your program and as the answer to this exercise please show the output that was produced.
After the code you wrote for the previous exercise, declare a fourth
variable that is initialized with the result of comparing the second
and third variables using only the == operator (without
using the * operator) and print out the value of that
fourth variable.
Compile and run your program and as the answer to this exercise please describe the error that occurred and explain briefly why it is happening.
Fix that error by using the * operator exactly once on the
appropriate reference variable in the comparison expression that is used
to initialize the fourth variable.
Compile and run your program and as the answer to this exercise please show the output the program produced.
Use the * operator again on each of the reference variables
involved in the comparison expression that is use to initialize the fourth
variable, so that the * operator is applied to one of them
once and to the other one of them twice.
Compile and run your program and as the answer to this exercise please explain briefly why the program produced the same output as in the previous exercise.
Make a copy of the statements you added to the main function's body for the previous exercise and put the original code into a conditional compilation block, e.g.,
#[cfg(oldexercise)] { let ... }, to preserve it.
Modify the copy that's outside the conditional compilation block so that
the first (non-reference) variable is declared mutable, and then just
after the first println! statement (1) assign it a different
value than it was initialized with, and (2) again print out the values of all
the variables that have been declared at that point in the program.
Compile and run your program and as the answer to this exercise please describe the error that occurred and explain briefly why it is happening.
Try to fix that error by making the second variable mutable (so what it aliases can change), and assigning it (instead of the first variable) the new value.
Compile and run your program and as the answer to this exercise please describe the error that occurred and explain briefly why it is happening.
Fix that error by removing the declarations of the third and fourth variables and all statements that depend on them, and then printing out only the value of the second variable before and after it is used to modify the value it has borrowed from the first one.
Compile and run your program and as the answer to this exercise please show the output that the program produced.
Put a conditional compilation block around the statements you added to the main function's body for the previous exercises, e.g.,
#[cfg(oldexercise)] { let ... }, to preserve them.
Below that conditional compilation block, declare a variable that is
initialized with the result of calling the to_string
function on a string literal, and then immediately after that use a
println! statement to print out the string.
In between the declaration and the println! statement
add an empty block expression (i.e., a pair of curly braces) and within
it (1) declare a variable that is a mutable reference to the string variable
that was declared outside of the block, (2) use a println!
statement to print out what that reference aliases, (3) assign a different
string value (i.e., obtained by calling the to_string function
on a different string literal) to a dereference of the reference, and
(4) use another println! statement to print out what the
reference now aliases.
Compile and run your program and as the answer to this exercise please show the output that the program produced.
For this studio, please turn in the following:
Page posted 17:15 Monday August 18th, 2025, by Chris Gill.