- cmp
- Less
- Equal
- Greater
- Ordering
Compare strings
- We can use the regular <, >, == operators to compare both strings and string slices.
- The cmp method returns a value from the Ordering enum.
examples/strings/compare-strings/src/main.rs
fn main() { let x = "abc"; let y = "abd"; let z = "abd"; println!("{}", x < y); println!("{:?}", x.cmp(y)); println!("{}", y == z) ; println!("{:?}", y.cmp(y)); println!(); let x = "abc".to_string(); let y = "abd".to_string(); let z = "abd".to_string(); println!("{}", x < y); println!("{:?}", x.cmp(&y)); println!("{}", y == z) ; println!("{:?}", y.cmp(&y)); }
true Less true Equal true Less true Equal