CSE 542S: Studio 2

Rust Types


In today's studio you will explore how to work with different types in Rust, and how Rust handles overflow, indexing, sorting, and other issues related to them.


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.


Required Exercises

  1. As the answer to the first exercise, list the names of the people who worked together on this studio.

  2. 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.

  3. 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.

  4. 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 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 542 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.

  5. 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.

  6. 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.

  7. 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.

Things to Turn In:

For this studio, please turn in the following:


Page posted Saturday July 6, 2024, and updated Friday August 30, 2024, by Chris Gill.