In today's studio you will explore how different collections in Rust are organized, and how to use them in different programming situations. In particular you will use methods of the collections themselves in combination with iterators, and will consider how ownership, move, and borrowing semantics may affect those techniques.
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., rustcollections
).
Change into the src
directory within that package and in
the main.rs
file that it contains modify the main
function
so that it (1) declares a string variable initialized with the text "Thanks,
Rosencrantz and gentle Guildenstern."
; (2) declares a vector of strings that
is initialized by calling collect
on the result of calling
map
with str::to_string
on the result of calling
split_whitespace()
on the string; and then (3) prints out the vector.
Compile and run your program, and as the answer to this exercise please show the code you wrote and the output the program produced.
In your program's main
function also declare a BTreeSet
of
strings that is initialized in the same way as the vector, and print out that
collection too. Compile and run your program, and as the answer to this exercise
please (1) show the output that was produced and (2) explain how and why the output
differed for the BTreeSet
vs. the vector.
In your program's main
function also declare a HashSet
of
strings that is initialized in the same way as the vector and the
BTreeSet
, and print out that collection too. Compile and run your
program, and as the answer to this exercise please (1) show the output that was
produced and (2) explain how and why the output differed for the
HashSet
vs. the BTreeSet
or the vector.
Modify your program's main
function so that instead of using
"Thanks, Rosencrantz and gentle Guildenstern."
to initialize the
string variable, it uses "She sells sea shells by the sea shore."
Compile and run your program, and as the answer to this exercise please (1) show
the output that was produced and (2) explain why the HashSet
and
BTreeSet
collections ended up containing fewer items than the vector.
In your program's main
function declare a BTreeMap
of
usize
and String
, then for each item in the vector
insert the item's index and a clone of its value into the BTreeMap
,
and then print out the BTreeMap
. Compile and run your program, and as
the answer to this exercise please (1) show the output that was produced and (2)
explain which other collection's contents are most similar to those of that
BTreeMap
, and why.
In your program's main
function declare another BTreeMap
but with the key and value types reversed (i.e., of String
and
usize
), then for each item in the vector insert a clone of the item's
value and the item's index into the second BTreeMap
, and then print
out the second BTreeMap
. Compile and run your program, and as the
answer to this exercise please (1) show the output that was produced and (2) explain
which other collection's contents are most similar to those of the second
BTreeMap
, and why.
When initializing collections from a string, it was not necessary to clone the items
before they went into the collections, but when initializing or updating a
collection from another collection it was necessary to clone non-copy items. Modify
your program by removing one of the calls to the clone
method, and then
try to compile and run your program. As the answer to this exercise, please (1) show
the error that occurred, and (2) explain briefly why that cloning was needed.
For this studio, please turn in the following:
Page posted Monday October 21, 2024, by Chris Gill.