stringsortingrust

What sort order does Rust's built-in cmp for &str use?


As I understood from this question, there are many ways to interpret the order of two strings.

Rust provides a default implementation for Ord for str, but I can't find the answer to these questions:


Solution

  • Just read the source!

    The documentation generated by RustDoc -- which is used for the officially hosted documentation, among others -- always include a link to the source:

    impl Ord for str {
        #[inline]
        fn cmp(&self, other: &str) -> Ordering {
            self.as_bytes().cmp(other.as_bytes())
        }
    }
    

    The answer, thus, is that for equality & ordering, Rust considers a str as just a slice of bytes.