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., rustmacros
).
Change into the src
directory within that package and in the
main.rs
file that it contains, modify the main
function
so that it checks how many command line arguments were passed to it, and if there
were any besides the name of the program itself, prints out an appropriate
usage message using the program's name.
Compile your program, and run it with and without additional command line
arguments. As the answer to this exercise please show the code for the
main
function and the output that was produced by each run.
Above the main
function in the main.rs
file, use a call
to macro_rules!
to define a macro named check_cmd_line
that has a single rule with an empty pattern and an empty template, i.e.
() => {};
Move the code out of the main
function and into the template of that
rule, and in the main function, call the macro.
Compile your program and run it with and without additional command line arguments,
and confirm that it produces the same outputs in each case respectively, as in the
previous exercise. As the answer to this exercise please show the code for the
main
function and the macro.
Above the macro definition in the main.rs
file, declare a public
static variable of type std::sync::atomic::AtomicBool
that is
initialized to true
, and modify the code in the macro rule's template
so that it only prints out the usage message if (in addition to the wrong number of
command line arguments having been provided to the program) the value of the public
static atomic variable is true
.
Compile your program and run it with and without additional command line arguments, and confirm that it produces the same outputs in each case respectively, as in the previous exercise. As the answer to this exercise please show the code for the declaration and initialization of the public static atomic variable, and the macro.
Modify the main
function in the main.rs
file so that
after it calls check_cmd_line!
it calls check_cmd_line!
a second time.
Compile your program and run it with additional command line arguments, and confirm that it produces the usage message twice. As the answer to this exercise, please show the output that was produced.
Add two more rules to the definition of the check_cmd_line
macro:
one with the pattern (true)
whose template sets the value of the public
static atomic variable to true
, and one with the pattern
(false)
whose template sets the value of the public static atomic
variable to false
.
Modify the main
function in the main.rs
file so that
it calls check_cmd_line!
three times: first with no arguments, then with
the argument false
, and then again with no arguments.
Compile your program and run it with additional command line arguments,
and confirm that it produces the usage message only once. As the answer to this
exercise, please show the main
function and the definition of the
check_cmd_line
macro.
For this studio, please turn in the following:
Page posted Wednesday, November 13, 2024, by Chris Gill.