1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2024-11-08 09:09:17 +01:00
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 13:16:32 +02: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 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),
}
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
}
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
}