2020-05-19 18:47:44 +02:00
|
|
|
// This is a quiz for the following sections:
|
2019-11-11 13:46:42 +01:00
|
|
|
// - Strings
|
2022-07-14 13:16:32 +02:00
|
|
|
// - Vecs
|
|
|
|
// - Move semantics
|
|
|
|
// - Modules
|
|
|
|
// - Enums
|
2023-05-29 19:39:08 +02: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 13:16:32 +02:00
|
|
|
// - Uppercase the string
|
|
|
|
// - Trim the string
|
|
|
|
// - Append "bar" to the string a specified amount of times
|
2024-06-26 02:25:59 +02:00
|
|
|
//
|
2022-07-14 13:16:32 +02:00
|
|
|
// The exact form of this will be:
|
2024-07-03 15:26:05 +02:00
|
|
|
// - The input is going to be a Vector of 2-length tuples,
|
2022-07-14 13:16:32 +02:00
|
|
|
// the first element is the string, the second one is the command.
|
2024-06-26 02:25:59 +02:00
|
|
|
// - The output element is going to be a vector of strings.
|
2015-09-21 00:31:41 +02:00
|
|
|
|
2024-05-22 15:04:12 +02:00
|
|
|
enum Command {
|
2022-07-14 13:16:32 +02:00
|
|
|
Uppercase,
|
|
|
|
Trim,
|
|
|
|
Append(usize),
|
2019-11-11 13:46:42 +01:00
|
|
|
}
|
2022-07-14 13:16:32 +02:00
|
|
|
|
|
|
|
mod my_module {
|
|
|
|
use super::Command;
|
|
|
|
|
2024-07-12 16:29:41 +02:00
|
|
|
// TODO: Complete the function as described above.
|
2024-06-26 02:25:59 +02:00
|
|
|
// pub fn transformer(input: ???) -> ??? { ??? }
|
2015-09-21 00:31:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-17 22:46:21 +02:00
|
|
|
fn main() {
|
|
|
|
// You can optionally experiment here.
|
|
|
|
}
|
|
|
|
|
2022-07-14 13:16:32 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-11-24 20:20:59 +01:00
|
|
|
// TODO: What do we need to import to have `transformer` in scope?
|
2024-06-26 02:25:59 +02:00
|
|
|
// use ???;
|
2022-07-14 13:16:32 +02:00
|
|
|
use super::Command;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_works() {
|
2024-06-26 02:25:59 +02: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 13:16:32 +02:00
|
|
|
}
|
2015-09-21 00:31:41 +02:00
|
|
|
}
|