Rust, Software Development

Ways To Compare Strings in Rust – Functions And Operators

Rust lang Convert Struct JSON

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

Using [crayon-60fb7a268e761876621158-i/] / [crayon-60fb7a268e763992266238-i/] To Compare [crayon-60fb7a268e764546666352-i/] and [crayon-60fb7a268e766155167004-i/] 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. If we open the file that comes with the Rust distribution, we get these codes.

The Eq trait extends the PartialEq trait ( 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. If we replace eq() with ne() in the code snippets, we will get a different output. 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 a little deeper, we will find the following code snippet in 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 [crayon-60fb7a268e79f887381621-i/] And [crayon-60fb7a268e7a1468575292-i/] 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. For the operators to work, operands must be of types that extend or implement both the Eq and PartialEq traits. With these operators, we don’t need to “pass” &str as we would with eq()  and ne().

Using [crayon-60fb7a268e7b2191906653-i/]

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


For more information, please see the notes.

Using [crayon-60fb7a268e7b7061639277-i/] For Sort Order

At some points in our codes, we would 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 [crayon-60fb7a268e7bd030847583-i/] For Sort Order

This method comes from the PartialOrd trait. Normally, we would use this trait to compare items based on some of 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 some 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.

Got comments, or suggestions? Please visit our Facebook page!

You Might Also Like