Rust, Software Development

Compare Strings in Rust – Functions And Operators

In any programming language, strings are essential. We’ll be hard-pressed to find any real-life application that doesn’t rely on strings. One of the everyday operations on strings is comparison. We can use the eq(), eq_ignore_ascii_case(), and == to compare strings in Rust.

Unlike other programming languages, strings in Rust have peculiar characteristics. Here are things you need to know about strings when learning Rust.

Using eq / ne To Compare String and &str Values

We use these functions to compare strings in Rust. Both of them are case-sensitive. Consider the following code snippets. The first code snippet compares String values. Although these are String values, we still need to pass their references to the other String to the function.

The codes generate the following output.

When it comes to &str values, the codes are similar.

The codes output the following.

The eq() function comes from the Eq trait. We get these codes when we open the string.rs file that comes with the Rust distribution.

The Eq trait extends the PartialEq trait ( cmp.rs). These are the main traits that allow our codes to compare strings in Rust.

Aside from the eq() function, we can also use the ne() (Not Equal) function. We will get a different output if we replace eq() with ne() in the code snippets. Consider the following codes for String values.

Finally, the following are codes for &str references.

Why do these functions accept references instead of String values? If we dig deeper, we will find the following code snippet in string.rs. These are the codes that compare strings in Rust.

If we drill into those highlighted methods, we’d see these codes. The implementations are on immutable string str, which is only accessible via &str.

For more information, please see the Eq trait and this blog post.

Using The == And != Operators

We can use these operators to compare strings in Rust when we use them on String or str values; they invoke the eq() and ne() methods accordingly. Operands must be of types that extend or implement both the Eq and PartialEq traits for the operators to work. With these operators, we don’t need to “pass” &str as we would with eq()  and ne().

Using eq_ignore_ascii_case

The eq_ignore_ascii_case() is a function on the str type. It compares strings in Rust without regard to their cases.

Output

For more information, please see the notes.

Using cmp For Sort Order

At some point in our codes, we want to know which string value is greater or less than another string value. To achieve that, we could use the cmp()  method, which comes from the Ord trait. Consider the following example.

The codes generate the following output.

From the sample codes, “apple” comes before “banana” in terms of sort order.

Using partial_cmp For Sort Order

This method comes from the PartialOrd trait. Typically, we would use this trait to compare items based on their attributes. For example, we compare instances of struct Person only by last names. However, for String and &str, the comparison reuses the cmp() method.

Other Rust String Types

And those are the usual ways to compare strings in Rust! However, there are other string types in Rust – OsString, and CString, and their reference type equivalents OsStr and Cstr, respectively. They are, in most ways, similar to String and &str.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like