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.
You will also explore Rust's features for representing, evaluating, and modifying text, including strings and string slices and methods provided for them as well as the different representations for text characters that Rust supports.
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.
In the directory for this studio, use the cargo new command to create a new
package (named e.g., rusttext).
Change into the src directory within that package and in
the main.rs file that it contains modify the main function
so that it moves the string slice "Hello, world!" out of the
println! macro and into the initialization of a declared string slice
variable.
Declare three more variables after that: (1) a variable initialized with the
result of calling the count method, on the result of calling the
filter method with a closure that applies
is_ascii_uppercase to each character,
on the result of calling the chars method on the string slice;
(2)a variable initialized with the
result of calling the count method, on the result of calling the
filter method with a closure that applies
is_ascii_lowercase to each character,
on the result of calling the chars method on the string slice; and
(3) a variable initialized with the result of subtracting the values of the first
two variables from the length of the string slice.
The println! macro should then print out the values of all variables
(including the string slice) along with text indicating what each numeric value
means. 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, declare a String
variable that is initialized to contain the characters of the original string
slice in reverse order, by calling the collect method on the result
of calling the rev method on the result of calling the
chars method on the original string slice.
Add a println! statement to print out the original string slice and
the new String variable. Compile and run your program, and as the
answer to this exercise please show the code you added and the output the program
produced.
Above your program's main function, declare a function that takes a
string slice (of type &str) and returns a bool value
indicating whether the passed string slice is a palindrome as follows: declare
two String variables, one initialized by calling the
collect method on the result of calling the chars method
on the string slice, and one initialized by calling the collect method
on the result of calling the rev method (which provides a reverse
iterator) on the result of calling the chars method on the string slice.
The method should then compare the strings for equality and return the result.
Modify your program's main function so that it uses that function
to determine whether or not different string slices like "kayak" and
"administration" are palindromes (as written, it only will handle
simple cases like those, though in subsequent exercises we will refine it)
and prints out each string slice and whether or not it's a palindrome.
Compile and run your program, and as the answer
to this exercise please show the output that was produced.
Modify your palindrome classification function so that it uses the
filter method with a closure that only preserves ASCII alphanumeric
values and removes all others (e.g., whitespace, punctuation, etc.), on the
forward iterator for the original string and the reverse iterator for the other
string, before calling the collect method on each of them.
Modify your program's main function so that it uses that function
to classify a string slice like "a7 6b b67a" and prints out that
string slice and whether or not it's a palindrome. Compile and run your program,
and as the answer to this exercise please show the output that was produced.
Modify your palindrome classification function so that instead of comparing the
initialized strings directly, it compares the results of calling the
to_lowercase method on each of them (so that it ignores capitalization).
Modify your program's main function so that it uses that function
to classify a string slice like "Madam, I'm Adam." and prints out that
string slice and whether or not it's a palindrome. Compile and run your program,
and as the answer to this exercise please show the output that was produced.
Above your program's main function, declare a function that takes a
char and returns a char, and uses a match expression
to perform the appropriate transformations that convert selected accented or
otherwise modified Latin-1 characters into their lowercase ASCII counterparts.
For example, one arm would match 'Ç' and return 'c'
and the catch-all arm would return the character that was passed into the function.
Modify your palindrome classification function so that it calls the
map method with that character conversion function, on the
iterators before calling the filter method on
each of them.
Modify your program's main function so that it uses that function
to classify a string slice like "Eh! Ça va, la vache?" and
prints out
that string slice and whether or not it's a palindrome. Compile and run your program,
and as the answer to this exercise please show the output that was produced.
Add an arm to your character conversion function's match expression, which matches
any character in the inclusive range 'È'..='Ë' and returns
'e'.
Modify your program's main function so that it classifies a string
slice like "Ésope reste ici et se repose." and prints out
that string slice and whether or not it's a palindrome. Compile and run your program,
and as the answer to this exercise please show (1) the output that was produced, and
(2) your program's entire code including the functions it uses.
For this studio, please turn in the following:
Page updated Tuesday September 22, 2026, by Chris Gill.