this post was submitted on 28 Dec 2021
4 points (100.0% liked)

Rust Programming

8159 readers
1 users here now

founded 5 years ago
MODERATORS
 

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?

top 2 comments
sorted by: hot top controversial new old
[โ€“] asonix@lemmy.ml 7 points 2 years ago (1 children)

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 have Vec<&str> or &[str] and there's no way to produce a Vec<&str> from a Vec<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

[โ€“] Reaton@lemmy.ml 1 points 2 years ago

Thanks for taking time to explain in details!