In today's studio you will explore how Rust macros can be defined and used to simplify and facilitate frequently occurring programming steps.
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., rustunsafe).
Change into the src directory within that package and in the
main.rs file that it contains, modify the main function
so that instead of printing out the string literal "Hello, world!"
it declares two integer variables initialized with different values
and prints them out.
Compile and run your program, and as the answer to this exercise please show the
code for the main function and the output that was produced.
Add a function to your program that takes mutable references to integers and
swaps their values. Modify your main function so that the variables
are declared to mutable, and after it prints them out calls the function to swap
their values, and prints them out again.
Compile and run your program, and as the answer to this exercise please show the code for both functions and the output that was produced.
Modify your program's main function so that it declares raw pointers
to mutable integers and passes those to the function that swaps values. Modify
the swapping function so that it takes raw pointers to mutable integers instead
of mutable references, and uses those raw pointers to swap the values.
Compile your program, and as the answer to this exercise please show the error(s) that occurred.
Mark the swapping function so that it is declared to be unsafe.
Compile your program, and as the answer to this exercise please show the error(s) that occurred.
Modify your main function so that its call to the swapping function
is made within an unsafe block.
Compile and run your program, and as the answer to this exercise please show the code for both functions and the output that was produced.
Based on your code for the previous exercise, please consider how it meets two necessary contracts to avoid undefined behavior: (1) each pointer always points to something that is valid and in scope, and (2) each referent (what each pointer points to) is well formed for its type and the address the pointer contains is properly aligned to it.
As the answer to this exercise please describe briefly how your code (including in the main function as well as in the swapping function) ensures that both of those contracts are met in all cases.
For this studio, please turn in the following:
Page updated Friday July 17, 2026, by Chris Gill.