Fall 2022 Rust Exam
Problem 1
Define a function named sumhow
that accepts three i32
parameters and returns true if and only if any two sum to the remaining. Do not use an if
statement, match
statement, or an explicit return
.
Problem 2
Define an enum
named Operation
with variants Add
, Subtract
, Multiply
, and Divide
. Also define a function evaluate
that returns the value of an arithmetic expression described by its three parameters: a first operand as an f32
, a second operand as an f32
, and an Operation
.
Problem 3
Define a function named teams
that accepts a Vec
of &str
and returns a Vec
of String
. The parameter vector is a list of programming contest team names. You reject any team whose name is 15 characters or longer. Accepted team names are converted to all-caps and returned in their original order. For example:
let pres = vec![
"SCREAMING_SNAKES",
"The Brute Force",
"tooStrong"
];
let posts = teams(pres);
// prints ["TOOSTRONG"]
println!("{:?}", posts);
Do not write any loops.
Problem 4
Define a function named show_friendlies
that accepts a Vec
of pairs. Each pair is a person's name as a String
and their friendliness index as an f64
. Print out the names of the people who have friendliness indices above the average, one per line. For example:
let people = vec![
("Alice".to_string(), 4.5),
("Bob".to_string(), 4.5),
("Carol".to_string(), 1.0),
];
show_friendlies(people);
// prints:
// Alice
// Bob
Problem 5
Define a struct named Rgb
that has fields r
, g
, and b
. Each is a u8
. Define associated functions white
and black
that return appropriate instances of Rgb
.
Problem 6
Define a function named halfway
that accepts two i32
values as parameters and returns the i32
that is exactly between them as an Option
. If no such number exists, return the Rust equivalent of null.
Problem 7
Define a main
function that accepts two command-line arguments: a string and an integer. It prints the string the given number of times.
Problem 8
Define a main
function that accepts a sequence of u32
integers as command-line arguments. Each pair of numbers represents an inclusive range. The function prints how many numbers are enclosed in the ranges. For example, ./main 8 12 10 20
prints 16, as there are 5 numbers in [8, 12] and 11 in [10, 20].