rustlings/solutions/quizzes/quiz2.rs

91 lines
2.7 KiB
Rust
Raw Normal View History

2024-06-26 01:25:59 +01: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:
// - Uppercase the string
// - Trim the string
// - Append "bar" to the string a specified amount of times
//
// The exact form of this will be:
2024-07-08 14:20:23 +01:00
// - The input is going to be a vector of 2-length tuples,
2024-06-26 01:25:59 +01:00
// the first element is the string, the second one is the command.
// - The output element is going to be a vector of strings.
enum Command {
Uppercase,
Trim,
Append(usize),
}
mod my_module {
use super::Command;
// The solution with a loop. Check out `transformer_iter` for a version
// with iterators.
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
let mut output = Vec::new();
2024-07-07 19:28:31 +01:00
for (string, command) in input {
2024-06-26 01:25:59 +01:00
// Create the new string.
let new_string = match command {
Command::Uppercase => string.to_uppercase(),
Command::Trim => string.trim().to_string(),
2024-07-07 19:28:31 +01:00
Command::Append(n) => string + &"bar".repeat(n),
2024-06-26 01:25:59 +01:00
};
// Push the new string to the output vector.
output.push(new_string);
}
output
}
// Equivalent to `transform` but uses an iterator instead of a loop for
// comparison. Don't worry, we will practice iterators later ;)
pub fn transformer_iter(input: Vec<(String, Command)>) -> Vec<String> {
input
.into_iter()
2024-07-07 19:28:31 +01:00
.map(|(string, command)| match command {
2024-06-26 01:25:59 +01:00
Command::Uppercase => string.to_uppercase(),
Command::Trim => string.trim().to_string(),
2024-07-07 18:49:38 +01:00
Command::Append(n) => string + &"bar".repeat(n),
2024-06-26 01:25:59 +01:00
})
.collect()
}
}
fn main() {
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
// Import `transformer`.
use super::my_module::transformer;
use super::my_module::transformer_iter;
use super::Command;
#[test]
fn it_works() {
for transformer in [transformer, transformer_iter] {
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",
]
);
}
}
}