rustrust string compare
Rust strings can be compared using the cmp()
method. This method takes two strings as parameters and returns an Ordering
enum which can be Less
, Equal
or Greater
.
Example
let string1 = "Hello";
let string2 = "World";
let result = string1.cmp(&string2);
println!("{:?}", result);
Output example
Less
The cmp()
method compares the two strings lexicographically and returns the result as an Ordering
enum. The Ordering
enum can be Less
if the first string is lexicographically less than the second string, Equal
if the two strings are equal, and Greater
if the first string is lexicographically greater than the second string.
Helpful links
More of Rust
- How to replace a capture group using Rust regex?
- How to use regex to match a double quote in Rust?
- How to use regex with bytes in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to calculate the sum of a Rust slice?
- How to convert a vector to a Rust slice?
- How to convert a slice into an iter in Rust?
- How to match the end of a line in a Rust regex?
- How to use non-capturing groups in Rust regex?
- How to perform matrix operations in Rust?
See more codes...