-5

What's the idiomatic way to compare string length in Rust, accounting for the case where the strings are of equal length?

There is a snippet of code in the lifetimes part of the Rust book but it simply returns the latter string if the given strings are of equal length:

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}

It has been pointed out that .len() counts the bytes rather than Unicode characters. The desired answer should include a function which returns the longest string in the case that one of the strings is longer in terms of Unicode characters; or something else in the case that the strings are of equal length.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 2
    Was it really necessary to ask how to compare two integers ? And the "length" of a string is rarely an interesting information. – Denys Séguret Oct 07 '19 at 12:15
  • Based on your self-answer, I'm thinking this question is a duplicate of [Can I use '<' and '>' in match?](https://stackoverflow.com/questions/47852269/can-i-use-and-in-match) (read all the answers). However, I don't know how I could have figured that out from reading the question alone, so I'm voting "unclear". – trent Oct 07 '19 at 12:32
  • If you're interested in the number of chars, for example, you could compare `a.chars().count()` to `b.chars().count()`, or, if you may have very different lengths, do [this](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=08a80cd13331d3060249659255d63b90) – Denys Séguret Oct 07 '19 at 12:43
  • 1
    See also [Get the String length in characters in Rust](https://stackoverflow.com/q/46290655/155423) – Shepmaster Oct 07 '19 at 12:51
  • @DenysSéguret sorry, I'm new and just following along with the book and didn't find a similar enough question. I'd like to have been able to search stackoverflow and be able to find the answer to the simple question. I couldn't, that's why I submitted this question. – mysterybear Oct 07 '19 at 12:55
  • 1
    The question asks how to compare two string lengths, then provides a piece of code that does exactly that. If you hadn't answered it yourself, how could a potential answerer have divined what answer it was you wanted? Can you edit the question to be more explicit about what's missing in the code? It's logical that "accounting for the case where the strings are of equal length" might be answered by "use `>=` instead of `>`" -- that's what I assumed you meant at first. – trent Oct 07 '19 at 16:16
  • @trentcl I asked what the *idiomatic* way of doing this was. The implication was that I knew how to naively do this (well, I could read the code I found in the book), but I wanted to know how to do it in the kind of robust manner you might find in actual use in a library or something (hence doing-something-different when the strings are of equal length). – mysterybear Oct 13 '19 at 08:06

1 Answers1

3
use std::cmp::Ordering;

fn longest<'a>(a: &'a str, b: &'a str) -> Option<&'a str> {
  match a.chars().count().cmp(&b.chars().count()) {
    Ordering::Equal => None,
    Ordering::Greater => Some(a),
    _ => Some(b),
  }
}