rust-itertools / itertools

Extra iterator adaptors, iterator methods, free functions, and macros.
https://docs.rs/itertools/
Apache License 2.0
2.62k stars 298 forks source link

feature request: split_vec() #935

Closed amab8901 closed 1 month ago

amab8901 commented 1 month ago

current code:

let string_vec = string.split(',').map(String::from).collect_vec();
dbg!(string_vec);

desired code:

let string_vec = string.split_vec(',');
dbg!(string_vec);
Philippe-Cholet commented 1 month ago

This is outside the scope of our library as split_vec would be a String method and not an iterator one.

If this is something you do a lot then you can write your own trait, something like (untested but seems legit to me)

trait StringExt: AsRef<str> {
    fn split_vec(&self, sep: char) -> Vec<String> {
        self.as_ref().split(sep).map(String::from).collect()
    }
}

impl<T: ?Sized + AsRef<str>> StringExt for T {}