rustlings/exercises/quizzes/quiz2.rs

64 lines
1.6 KiB
Rust
Raw Permalink Normal View History

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