Skip to main content

canonical_strings

Function canonical_strings 

Source
pub fn canonical_strings<S: AsRef<str>>(items: &mut [S])
Expand description

Put a collection of plain strings in canonical order, in place.

A Vec<String> has no group, no rank and no identity apart from the string itself, so it cannot implement Ordered without a newtype nobody wants. It still has an order, and before this existed every caller picked one of two: .sort(), which is Ord for str and puts case-107 before case-9, or a hand-written sort_by(|a, b| natural_cmp(a, b)), which is this function spelled out again at the call site. The second is the duplication project.canonical-order exists to prevent, and the first is a different order from the one every other collection in the crate is shown in.

use majordomus_cli::order::canonical_strings;

let mut cases = vec!["case-107".to_string(), "case-9".to_string(), "case-10".to_string()];
canonical_strings(&mut cases);
assert_eq!(cases, ["case-9", "case-10", "case-107"]);