mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 15:26:29 +00:00
quiz2: Use repeat
This commit is contained in:
parent
6469e9734b
commit
0f4cb94cfe
1 changed files with 3 additions and 8 deletions
|
@ -24,17 +24,12 @@ mod my_module {
|
||||||
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
|
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
|
||||||
let mut output = Vec::new();
|
let mut output = Vec::new();
|
||||||
|
|
||||||
for (mut string, command) in input {
|
for (string, command) in input {
|
||||||
// Create the new string.
|
// Create the new string.
|
||||||
let new_string = match command {
|
let new_string = match command {
|
||||||
Command::Uppercase => string.to_uppercase(),
|
Command::Uppercase => string.to_uppercase(),
|
||||||
Command::Trim => string.trim().to_string(),
|
Command::Trim => string.trim().to_string(),
|
||||||
Command::Append(n) => {
|
Command::Append(n) => string + &"bar".repeat(n),
|
||||||
for _ in 0..n {
|
|
||||||
string += "bar";
|
|
||||||
}
|
|
||||||
string
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Push the new string to the output vector.
|
// Push the new string to the output vector.
|
||||||
|
@ -49,7 +44,7 @@ mod my_module {
|
||||||
pub fn transformer_iter(input: Vec<(String, Command)>) -> Vec<String> {
|
pub fn transformer_iter(input: Vec<(String, Command)>) -> Vec<String> {
|
||||||
input
|
input
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(mut string, command)| match command {
|
.map(|(string, command)| match command {
|
||||||
Command::Uppercase => string.to_uppercase(),
|
Command::Uppercase => string.to_uppercase(),
|
||||||
Command::Trim => string.trim().to_string(),
|
Command::Trim => string.trim().to_string(),
|
||||||
Command::Append(n) => string + &"bar".repeat(n),
|
Command::Append(n) => string + &"bar".repeat(n),
|
||||||
|
|
Loading…
Reference in a new issue