Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@keturiosakys
Last active October 10, 2022 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keturiosakys/d2a6c84822004b75d1449b937c71f2c5 to your computer and use it in GitHub Desktop.
Save keturiosakys/d2a6c84822004b75d1449b937c71f2c5 to your computer and use it in GitHub Desktop.
Truncator
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