In today's studio you will explore how different kinds of expressions can be used in Rust, and how issues like precedence, ownership, and generation and propagation of values through expressions.
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., rustexpressions).
Change into the src directory within that package and edit the
main.rs file that it contains.
Modify the main function so that it prints out the value
of an arithmetic expression that uses only numbers, the addition operator,
and the multiplication operator, in a manner that depends on the precedence
of the operators.
Compile and run your program and as the answer to this exercise please (1) show code you wrote and the output that was produced by the program, and (2) explain briefly which of the arithmetic operators has higher precedence and why that explanation is consistent with the value 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)] { println! ... }, to preserve it.
Modify the copy that's outside the conditional compilation block so that
the program declares two variables with different numeric values, and then
prints out the result of an if { } else { }
expression that compares the values using the less than operator
and gives the larger of the
two values.
Compile and run your program and as the answer to this exercise please show the code you wrote for it and the output of the program.
Modify your program so that one of the blocks in the
if { } else { } expression has a semicolon before the closing
brace.
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.
Remove that semicolon and then 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.
Paste the statements you copied below that block, and modify them so that the
program declares a variable that is a tuple of the values of the variables
declared in the previous exercise, and then uses match to
destructure the tuple into appropriately named references so that the
program prints out the larger of the two values in the tuple using the
if { } else { } expression from the previous exercise.
Compile and run your program and as the answer to this exercise please show the code you wrote for it and the output of the program when it ran.
Put the code from the previous exercise into a conditional compilation block, e.g.,
#[cfg(oldexercise)] { let ... }, to preserve it.
Below that block declare a mutable vector and then in a for
loop that iterates through a range of unsigned integers from 0 up to but not
including 10, push those unsigned integers into the vector. The program
should then print out the entire contents of the vector using
"{:?}" in a println! statement.
Compile and run your program and as the answer to this exercise please show the code you wrote for it and the output of the program when it ran.
After the code from the previous exercise, add another for
loop in which a variable iterates over the values in the vector
(i.e., not using a reference or direct indexing to
do that) as in
for j in v {...}
and tries to print out each element of the vector individually. Also make
sure that there is another expression after the end of that for
loop, which uses the vector variable (e.g., another println! that
prints it out 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.
Modify the code from the previous exercise so that it iterates over a reference to the vector (so that the loop variable is also a reference) and prints out each element in the vector.
Compile and run your program and as the answer to this exercise please show the code you wrote for it and the output of the program when it ran.
For this studio, please turn in the following:
Page posted 17:25 Monday August 18th, 2025, by Chris Gill.