rustlings/exercises/quizzes/quiz2.rs

64 lines
1.6 KiB
Rust
Raw Normal View History

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