-
-
Save George-Payne/af4b2f64757bbbcf6e759eefd770e343 to your computer and use it in GitHub Desktop.
Text chunking
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 chunk_text(width: i32, text: &str) -> Vec<String> { | |
text.split_whitespace().fold(vec![], |mut acc, word| { | |
let length = acc.len(); | |
if length > 0 && &acc[length - 1].len() + word.len() + 1 <= width as usize { | |
acc[length - 1] = [&acc[length - 1], word].join(" "); | |
} else { | |
acc.push(word.to_string()); | |
} | |
acc | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment