I want to use the crate human-sort but it only takes [&str] and I’m currently working with Vec<String>.
Is this possible to make the conversion Vec<String> to [&str]? and in the other way?
I want to use the crate human-sort but it only takes [&str] and I’m currently working with Vec<String>.
Is this possible to make the conversion Vec<String> to [&str]? and in the other way?
First, it doesn’t take an array as input, it takes a slice as input. You can turn a
Vec<&str>
into a slice&[&str]
by borrowing it.Second, the
human-sort
crate’s api is bad, because rarely to people haveVec<&str>
or&[str]
and there’s no way to produce aVec<&str>
from aVec<String>
without creating a new vector that borrows from the first.let mut strs = Vec::new(); for s in strings.iter() { strs.push(s); } human_sort::sort(&mut strs);
What human-sort should have done was accept
&[T] where T: Deref<Target = str>
which would allow passing a Vec of String or a Vec of &str.Feel free to open a PR against human-sort, but it looks like it hasn’t been updated in a couple years so it might not be maintained anymore
Thanks for taking time to explain in details!