In today's studio you will explore a variety of ideas involving iterators, iterator adapters, and closures, with a focus on a Rust implementation of the Sieve of Eratosthenes algorithm for finding prime numbers (inspired by the example at https://rustp.org/number-theory/sieve-of-eratosthenes/).
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., rustiterators
).
Change into the src
directory within that package and in
the main.rs
file that it contains modify the main
function
so that it uses a for
loop and the format!
macro to print out all of the unsigned integers from 0 to 99 inclusive with 10
whitespace delimited values per line.
Compile and run your program, and as the answer to this exercise please show your code and the output the program produced.
Inside your program's main
function, declare a vector of type
bool
, and for each unsigned integer in the iteration range for the
previous exercise
push
the value true
into the vector if and only if the
integer is either 2
or is an odd integer greater than 2
(and otherwise push
the value false
into the vector).
Modify your program's main
function so that it only prints out integer
values that are marked true
at the corresponding position in the vector.
Compile and run your program, and as the answer to this exercise please show the
output that was produced.
Move the logic that pushes bool
values into the vector out of the
for
loop and instead initialize the vector in its declaration line,
by calling the collect
method on the result of calling the
map
method with an appropriately defined closure, on the range of
unsigned integers.
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 that initializes the vector.
Modify your program's main
function so that it iterates over the
range of unsigned integers (in increasing order) and if the vector at an integer's
position is true, marks all vector elements whose indices are larger multiples of
that integer (within the range) false.
Compile and run your program, and as the answer to this exercise please show the output the program produced.
Move the code that marks multiples false into a call to the for_each
method on the unsigned integer range starting at 3
, by passing
an appropriately defined closure into the for_each
method.
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 that uses the for_each
method with the closure, to mark multiples
false in the vector.
Optimize the logic for crossing off multiples so that it starts at the square of the number whose multiples are being crossed off and advances by twice the number each time. 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 updated code with that optimization.
For this studio, please turn in the following:
Page posted Sunday October 20, 2024, by Chris Gill.