2020-05-19 12:47:44 -04:00
|
|
|
// This is a quiz for the following sections:
|
2019-11-11 07:46:42 -05:00
|
|
|
// - Strings
|
2022-07-14 07:16:32 -04:00
|
|
|
// - Vecs
|
|
|
|
// - Move semantics
|
|
|
|
// - Modules
|
|
|
|
// - Enums
|
2023-05-29 13:39:08 -04:00
|
|
|
//
|
|
|
|
// Let's build a little machine in the form of a function. As input, we're going
|
|
|
|
// to give a list of strings and commands. These commands determine what action
|
|
|
|
// is going to be applied to the string. It can either be:
|
2022-07-14 07:16:32 -04:00
|
|
|
// - Uppercase the string
|
|
|
|
// - Trim the string
|
|
|
|
// - Append "bar" to the string a specified amount of times
|
2024-06-25 20:25:59 -04:00
|
|
|
//
|
2022-07-14 07:16:32 -04:00
|
|
|
// The exact form of this will be:
|
2024-06-25 20:25:59 -04:00
|
|
|
// - The input is going to be a vector of a 2-length tuple,
|
2022-07-14 07:16:32 -04:00
|
|
|
// the first element is the string, the second one is the command.
|
2024-06-25 20:25:59 -04:00
|
|
|
// - The output element is going to be a vector of strings.
|
2015-09-20 18:31:41 -04:00
|
|
|
|
2024-05-22 09:04:12 -04:00
|
|
|
enum Command {
|
2022-07-14 07:16:32 -04:00
|
|
|
Uppercase,
|
|
|
|
Trim,
|
|
|
|
Append(usize),
|
2019-11-11 07:46:42 -05:00
|
|
|
}
|
2022-07-14 07:16:32 -04:00
|
|
|
|
|
|
|
mod my_module {
|
|
|
|
use super::Command;
|
|
|
|
|
2024-06-25 20:25:59 -04:00
|
|
|
// TODO: Complete the function.
|
|
|
|
// pub fn transformer(input: ???) -> ??? { ??? }
|
2015-09-20 18:31:41 -04:00
|
|
|
}
|
|
|
|
|
2024-04-17 16:46:21 -04:00
|
|
|
fn main() {
|
|
|
|
// You can optionally experiment here.
|
|
|
|
}
|
|
|
|
|
2022-07-14 07:16:32 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-11-24 14:20:59 -05:00
|
|
|
// TODO: What do we need to import to have `transformer` in scope?
|
2024-06-25 20:25:59 -04:00
|
|
|
// use ???;
|
2022-07-14 07:16:32 -04:00
|
|
|
use super::Command;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_works() {
|
2024-06-25 20:25:59 -04:00
|
|
|
let input = vec![
|
|
|
|
("hello".to_string(), Command::Uppercase),
|
|
|
|
(" all roads lead to rome! ".to_string(), Command::Trim),
|
|
|
|
("foo".to_string(), Command::Append(1)),
|
|
|
|
("bar".to_string(), Command::Append(5)),
|
|
|
|
];
|
|
|
|
let output = transformer(input);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
output,
|
|
|
|
[
|
|
|
|
"HELLO",
|
|
|
|
"all roads lead to rome!",
|
|
|
|
"foobar",
|
|
|
|
"barbarbarbarbarbar",
|
|
|
|
]
|
|
|
|
);
|
2022-07-14 07:16:32 -04:00
|
|
|
}
|
2015-09-20 18:31:41 -04:00
|
|
|
}
|