Spring 2024 Rust Exam

Problem 1

Write function is_similar to accept the width-height dimensions of two rectangles as pairs of f64. It returns true if and only if the rectangles are similar. Two rectangles are similar if one can be rotated and uniformly scaled to match the other. For example: is_similar((1.0, 2.0), (4.0, 2.0))true. Consider two floats equal if they are within 0.001 of each other.

Problem 2

A media player supports the following speeds: Normal, Double, Triple, and Half. Define an enum named Speed with these variants. Have it derive Debug. Define also a struct named Stretch with two fields. Field seconds is an f64 measuring the normal length of the stretch. Field speed indicates how fast the stretch should be played. Give it an instance method duration that returns the stretch's effective length as an f64 given its playback speed.

Problem 3

Write a function round_to_nth that accepts a number as an f64 and a denominator \(n\) as a u32. It rounds the number to the nearest \(n^\textrm{th}\). For example:

Problem 4

Define a main function that expects a u32 as a command-line argument. It prints all the ways two unsigned integers can be multiplied together to form the argument. For example, running the program on parameter 28 yields this output:

1 * 28 = 28
2 * 14 = 28
4 * 7 = 28
7 * 4 = 28
14 * 2 = 28
28 * 1 = 28

Problem 5

Write function sort_from that accepts ownership of a vector of string slices. It returns a new Vec of index-string slice pairs. The pairs are sorted by the string component, but each index is the slice's original index. For example, sorting the strings ["yellow", "blue", "gravy"] returns [(1, "blue"), (2, "gravy"), (0, "yellow")].

Use iterators and higher-order functions. Do not use loops.

Problem 6

Define a struct named Heading that owns a string named text and an unsigned integer named level. Define an associated function named new that accepts a string and integer as parameters and returns a new instance of the struct. Define also an instance method named to_html that returns the heading as an HTML tag. For example Heading.new("Bikeable Cities".to_string(), 2).to_html()"<h2>Bikeable Cities</h2>".

Problem 7

Write function whence that accepts two borrowed vectors of i32. Assume the vectors contain the same unique numbers, but in a different order. It returns a vector that corresponds to the first, but each element is the index of the corresponding number in the second vector. For example, if the first vector is [56, 23, 38] and the second is [23, 56, 38], it returns [1, 0, 2].

Use iterators and higher-order functions. Do not use loops.

Problem 8

Define a struct named Tally that has these four fields:

The counts are all u32.

Define an associated function new that accepts a target value and returns a new instance of the struct in which all counts are 0. For T that implements the PartialOrd trait, define also an instance method guess that accepts a mutable reference to its receiver and a reference to a guessed T value. It updates the appropriate counter.