-
-
Save keturiosakys/d2a6c84822004b75d1449b937c71f2c5 to your computer and use it in GitHub Desktop.
Truncator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let truncated_phrase = truncate("holler the world", 2); | |
dbg!(truncated_phrase); | |
} | |
fn truncate(phrase: &str, length: i32) -> String { | |
let truncated_phrase: Vec<String> = phrase | |
.split(|char: char| !char.is_alphabetic()) | |
.map(|word| { | |
let mut truncated_word = word.to_string(); | |
let mut counter = (word.len() as i32) - length; | |
while counter > 0 { | |
truncated_word.pop(); | |
counter -= 1; | |
} | |
truncated_word | |
}) | |
.collect(); | |
truncated_phrase.join(" ") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment