CSE 542S: Studio 16

Strings and Text


In today's studio you will 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.


Required Exercises

  1. As the answer to the first exercise, list the names of the people who worked together on this studio.

  2. 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., 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

Things to Turn In:

For this studio, please turn in the following:


Page posted Wednesday October 23, 2024, by Chris Gill.