Spring 2023 Rust Exam

Problem 1

Define a function named word_count that accepts a string slice. It returns the number of words in the string as a usize. Words are runs of one or more non-whitespace characters separated by one or more whitespace characters. For example, the string " I forget everything that I don't need to turn into a story. " has 12 words.

Write this as a single expression. Do not write any loops or use an explicit return.

Problem 2

Define a function named longest_word that accepts a string slice. It returns the longest word found in the string as an Option<&str>. Words are runs of one or more non-whitespace characters separated by one or more whitespace characters. If there are no words, it returns the equivalent of null. If multiple words have the maximum length, it returns the one that appears first.

Write this as a single expression. Do not write any loops or use an explicit return.

Problem 3

Define an enum named Coin with variants Penny, Nickel, Dime, and Quarter. Have it automatically implement the Debug trait.

Give the Coin enum an instance method named worth. It returns the coin's worth in cents as a u32. It borrows its receiver rather than owns it.

Also define a function tally that accepts a borrowed Vec of Coin. It returns the total value of the coins as a u32.

Do not write any loops or explicit returns.

Problem 4

Define function divmod that accepts a numerator and denominator as u32 values and returns their quotient and remainder as a pair of u32.

Problem 5

Define a function named curve that accepts a borrowed Vec of pairs. Each pair is a student's name as a String and a test score as a u32. Print out a report of the students' names and their curved scores. The curve is applied by shifting all scores so that the highest score is bumped to 100. For example, if the highest score is 87, then all scores are bumped up by 13. Assume the vector contains at least one name-score pair.

Suppose Marisol scored 91, Yara 80, and Bunker 36. Then this report is printed:

Marisol: 100
Yara: 89
Bunker: 45

Problem 6

Define a struct named CellAddress that has fields column and row, both of type u8.

Define an associated function named new that accepts the column and row as parameters and returns a new instance of the struct.

Define an instance method named label that returns a String representation of the address. It borrows its receiver. The column presents as a single uppercase letter, with 0 mapping to A. For example CellAddress::new(1, 3).label() yields "B3". Assume the column is less than 26.

Problem 7

Define a function named halfters that accepts a borrowed Vec of i32. It returns a Vec of f64 values just like the parameter, except each value is 0.5 greater than its original value. For example, halfters(vec![7, 10, -99])vec![7.5, 10.5, -98.5].

Do not write any loops or explicit returns.

Problem 8

Define a main function that accepts a set of unordered i32 integers as command-line arguments. It prints in order the numbers between the minimum and maximum arguments that aren't present in the arguments. For example, ./main 1 10 6 3 prints the following output:

2
4
5
7
8
9

Assume that at least one number is present.