In today's studio you will explore how to work with built-in types in Rust, and how Rust handles overflow and other issues related to them. You will also explore structs the first of many user-defined abstractions we will consider in this course.
Please complete the following required exercises and any of the optional enrichment exercises you would like to try. 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., rusttypes).
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 a mutable 8
bit unsigned integer with initial value 1 and then (2) in a non-terminating
loop expression repeatedly doubles the value of that variable
and prints out the variable's value.
Build and run the program to see what the largest value is that is printed out before overflow occurs and the program panics, and as the answer to this exercise please show the program's output.
Make a copy of the loop expression you wrote for the previous
exercise and put the original code into a conditional compilation block, e.g.,
#[cfg(oldexercise)] { loop {...} }, to preserve it.
Modify the copy that's outside the conditional compilation block so that it
uses the checked_mul operation as the condition of a
while expression to double the variable's
value until overflow would occur the next time it was doubled. Build and run the
program and as the answer to this exercise again show the program's output.
Change into the top level directory you created for this studio, and in it use
the cargo new command to create a new package
(named e.g., ruststructs).
Change into the src directory within that package and edit the
main.rs file that it contains.
Outside of the main function, declare a struct item of type
Point with two fields of type f64.
Inside the main function, declare an immutable variable of type
Point whose fields are initialized with different floating
point values. Then, the main function should print out the value of
the point variable using "{}" for each of its fields, as in
p is (1.7, 0.1)
Compile and run your program and as the answer to this exercise please show the code you wrote and the output of the program.
Modify the statement that prints out the value of the point variable so that
it uses "{:?}" once (for the entire variable), instead of using
"{}" twice (for each of the variable's fields).
Compile and run your program and as the answer to this exercise please show
the error that was produced.
Annotate the Point declaration with the attribute
#[derive(Debug)].
Compile and run your program and as the answer to this exercise please show
the output the program produced.
Note that the code used in the previous exercise produced a warning about
dead code analysis ignoring derived implementations. Per the course
Programming Guidelines, although it is
possible to suppress that warning using #[allow(dead_code)], it
is better simply to address the warning directly if that is reasonable.
Modify the declaration of the Point variable so that it is
mutable, and then after the statement that prints it out add statements
that modify the values of its fields and another statement that prints
it out again. Compile and run your program and as the answer to this
exercise please show the output the program produced.
In between the struct declaration and the main function, add an
impl block and in it declare an associated public
new
function that takes no parameters and returns an instance of the struct
type that is initialized with value 0 in each of its fields.
Modify your main function so that it uses that function to initialize its
struct variable. Compile and run your program and as the answer to this
exercise please show the output the program produced.
Modify your struct declaration so that it takes a generic type parameter
T and uses that as the type of its fields. Then modify your
impl block for that struct so that it also takes a generic type
parameter T and uses it in the type of the struct and for the
types of the struct's fields. Then modify the new function
within the impl block so that it takes two arguments of the
parameterized type and uses those arguments to initialize the struct's fields.
Modify your main function so that it (1) uses the "turbofish" notation to
specify that the struct variable is parameterized with type f64,
and (2) provides appropriate floating point values for its fields in its call
to the (updated version of the) new function. Compile and run
your program and as the answer to this exercise please show the code you
wrote and the output the program produced.
Declare another mutable variable in your main function, which is
parameterized with type usize and is initialized by
providing appropriate unsigned integer values to a call to the (generic)
new function. Print out the value of that variable, change
the values of its fields, and then print it out again. Compile and run
your program and as the answer to this exercise please show the code you
added for this exercise, and the output the program produced.
In the generic implementation block for the struct, add an associated
public set method that takes a mutable reference to the
type-parameterized struct type as its first (self) argument,
and instances of the parameterized type as its second and third arguments.
The method should use the self argument to assign the first
argument to the first field of the struct, and assign the second argument
to the second field of the struct.
Modify your program's main function so that it uses the new set
method to modify the structs' fields' values, instead of modifying the
fields directly. Compile and run your program, and confirm that it produces
the same output as in the previous exercise. As the answer to this exercise
please show the code for the method you added, and explain briefly why the
dead code error that you had seen earlier does not occur, i.e., is it
necessary to access the fields directly, or is it sufficient to call a
method that accesses them?
In your rusttypes program, make a copy of the while
expression and put the original code into a conditional compilation block,
e.g.,
#[cfg(oldexercise)] { while {...} }, to preserve it.
Modify the copy that's outside the conditional compilation block so that it
uses the overflowing_mul operation within the while
expression's condition. Note that doing this will require you to
access the second element of the tuple that's returned. If you have to use
.1 to do that, please make sure to comment what is contained in
that position of the tuple, per the CSE 5402
Programming Guidelines entry about hard-coded numbers.
Within each iteration of the while expression still double and print out the variable's value, and in this exercise also count and print out how many doublings have been done without reaching overflow. Build and run the program and as the answer to this exercise show the program's output.
Make a copy of the while expression you wrote for the previous
exercise and put the original code into a conditional compilation block, e.g.,
#[cfg(oldexercise)] { while {...} }, to preserve it.
Modify the copy that's outside the conditional compilation block so that it
uses a for expression to iterate one more time than in the previous
exercise. Please note that iteration ranges in Rust are half-open
(specifically [first, last) a.k.a. "right-open" in mathematics),
i.e., from the first element up to but not including the last element of the
specified range. Modify the body of the for
expression so that is uses the saturating_mul operation to saturate
at the maximal value if overflow occurs, or if not to simply double the value.
Again have the body of the for expression print out the value of
the variable and a count of how many iterations have been done. Build and run
your program, and as the answer to this exercise please show the output that
was produced.
Make a copy of the for expression you wrote for the previous
exercise and put the original code into a conditional compilation block, e.g.,
#[cfg(oldexercise)] { for {...} }, to preserve it.
Modify the copy that's outside the conditional compilation block so that its
body uses the wrapping_mul operation to wrap around modulo the
representation range if overflow occurs, or if not to simply double the value.
Again have the body of the for expression print out the value of
the variable and a count of how many iterations have been done. Build and run
your program, and as the answer to this exercise please show the output that
was produced.
Make a copy of the for expression you wrote for the previous
exercise and put the original code into a conditional compilation block, e.g.,
#[cfg(oldexercise)] { for {...} }, to preserve it.
Modify the copy that's outside the conditional compilation block so that it
first declares a mutable vector and pushes the initial value of the variable
that will be doubled into the vector. Modify the body of the for
expression so that in addition to doubling the variable's value and printing out
the new value and a count of how many iterations have been done, it also pushes
the new value into the vector. After the body of the for
expression, the main function should call the sort function on the
vector and then print out the vector's (sorted) contents. Build 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 15:45 Monday August 18, 2025, by Chris Gill.