In today's studio you will explore variables' ownership of their values for different types in Rust, and how and when ownership may be transfered for certain types.
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., rustownership).
Change into the src directory within that package and edit the
main.rs file that it contains.
Modify the main function so that it (1) declares two immutable 8
bit unsigned integers with different initial values, (2) declares a third
immutable 8 bit unsigned integer that is initialized using the second one, and
then (3) prints out the values of the three variables.
Compile and run your program and as the answer to this exercise please show the output that was produced.
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 (1)
the variables are all of immutable String type, and (2) the
first two (e.g., a, b) are initialized by applying the to_string function to
different string literals. The third String should be initialized
using the second one (e.g., c = b), and immediately after that the main function should
print out the values of the three String variables.
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 making the second String variable mutable,
and then (immediately after it is used to initialize the third
String variable) assigning it the result of applying the
to_string function to a different string literal than was used
to initialize the previous variables.
Compile and run your program and as the answer to this exercise please show the output that was produced.
Modify the code so that after it prints out all three variables, the main
function assigns the third one to the second one (e.g., b = c) and then prints out all three
variables again.
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 making the third String variable mutable,
and immediately after it is assigned to the second String
variable assigning it the result of applying the to_string
function to a different string literal than any of the previous ones.
Compile and run your program and as the answer to this exercise please show the output that was produced.
At the end of the main function, (1) declare a mutable vector, (2) push all
three String variables into it, and then (3) print out the
contents of the vector.
Compile and run your program and as the answer to this exercise please show the output that was produced.
At the end of the main function, (1) declare another immutable
String variable that is initialized with the result of indexing at
the last valid string position in the vector, and (2) print out the contents of
the vector and that String 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 changing that String variable's initialization
to use the result of applying the pop function to the vector
instead of indexing into the vector.
Compile and run your program and as the answer to this exercise please show the output that was produced.
For this studio, please turn in the following:
Page posted 17:10 Monday August 18th, 2025, by Chris Gill.