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?

  • Aode (He/They)
    link
    fedilink
    72 years ago

    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