Я думаю, что ваше решение хорошо, но, возможно, split_at
может вам помочь.
Один из способов сделать это:
fn word_split2(word: &str) -> Vec<String> {
word
.char_indices()
.flat_map(|(i, _ch)| {
let (left, right) = word.split_at(i);
vec![left, right].into_iter() // array would be better than vec!, but does not support into_iter as of now
})
.map(|s| s.to_string())
.collect()
}
Ваш второй версия может быть записана как:
fn word_split2(word: &str) -> Vec<String> {
let (splits1, splits2) = word
.char_indices()
.map(|(i, _ch)| {
let (left, right) = word.split_at(i);
(left.to_string(), right.to_string())
})
.unzip::<_, _, Vec<_>, Vec<_>>();
[splits1, splits2].concat()
}