How to compare strings in Rust

Use the standard comparison operators (`==`, `!=`, `<`, `>`) directly on string types, as Rust implements the `PartialEq` and `PartialOrd` traits for `&str`, `String`, and `&String`.

Use the standard comparison operators (==, !=, <, >) directly on string types, as Rust implements the PartialEq and PartialOrd traits for &str, String, and &String. You can compare owned String values, string slices (&str), or mix them without manual conversion, and the comparison is always lexicographical based on Unicode code points.

Here is a practical example comparing different string types and handling case sensitivity:

fn main() {
    let owned = String::from("hello");
    let slice = "hello";
    let other = String::from("world");

    // Comparing owned String with a string slice works directly
    if owned == slice {
        println!("Match found!");
    }

    // Lexicographical comparison (case-sensitive)
    if "apple" < "banana" {
        println!("'apple' comes before 'banana'");
    }

    // Case-insensitive comparison requires manual handling
    let s1 = "Rust";
    let s2 = "rust";
    
    if s1.eq_ignore_ascii_case(s2) {
        println!("Match ignoring ASCII case");
    }
    
    // For full Unicode case folding (e.g., German ß), use the unicode-segmentation crate
    // or convert to lowercase first: s1.to_lowercase() == s2.to_lowercase()
}

For case-insensitive comparisons, eq_ignore_ascii_case is the most efficient built-in method for ASCII text. If you need full Unicode normalization (handling characters like the German sharp S or Turkish dotted I), convert both strings to lowercase using to_lowercase() before comparing, or use the unicode-segmentation crate for more complex linguistic rules. Remember that String comparisons borrow the underlying data automatically when compared against &str, so you don't need to call .as_str() explicitly unless you are passing the value to a function expecting a specific lifetime.

If you are comparing large strings in a performance-critical loop, ensure you are comparing references (&str) rather than moving owned String values to avoid unnecessary allocations. The standard operators are optimized and safe for production use.