Hello! Welcome to this lesson on String Methods and Ownership in Rust. In the previous lesson, we explored string data types, including string literals, the String type, references, and string slices. Today, we'll delve deeper into string manipulation by learning various string methods while understanding Rust's unique ownership model. By the end of this lesson, you will have a strong grasp of how to manipulate strings and understand how ownership affects strings in Rust.
Rust provides multiple ways to concatenate strings. The push_str method adds a string slice to the end of another String. The variable passed into push_str must be a string literal/slice or a reference to a String. Don't forget to declare the String as mutable with mut.
In this example:
- We created a mutable
Stringcalledgreeting. - We used the
push_strmethod to append the string literal" Rust"togreeting. - We created a
Stringcalledworldand concatenated it togreeting
push_str is useful when you want to add a string slice to an existing string.
Another way to concatenate strings is by using the + operator. This method is slightly different as it moves ownership of the original string. In addition, the second variable must be a reference.
In this example:
- We created two
Stringvariables,helloandexplorer. - We create a string literal called
rust - We used the
+operator to concatenate these strings intogreeting. - Note that after using the
+operator,hellocannot be used anymore because its ownership has been moved togreeting.
Understanding how ownership works in string concatenation is crucial. The + operator consumes the left operand's ownership, making it unavailable for further use.
Rust offers the len method to determine the length of a string. This method counts the number of bytes in the string, not the number of characters. All ASCII characters are a single byte, so the number of bytes in a string of ASCII characters is equal to the number of characters in the string.
Keep in mind that len returns the number of bytes. If your string contains multi-byte characters such as € or 汉, the length might be different from the number of characters.
Rust supports comparing strings for equality using the == operator and inequality using the != operator. These operators in Rust are designed to compare different types of strings, making it convenient to check if a string slice and a String contain the same value. Keep in mind that comparisons are case sensitive.
In this example:
word1 == word2comparesword1andword2using the==operator, which checks for equality. Since both have the value "Hello", it printstrue.word1 != word3comparesword1andword3using the!=operator, which checks for inequality. Since "Hello" is not equal to "HELLO" (comparison is case-sensitive), it printstrue.word1 == word4comparesword1(a string literal) with (a type) using the operator. Rust allows comparison between a string literal and a type, and since both contain the value "Hello", it prints
Besides equality, Rust also allows you to compare strings lexicographically. The string that comes first alphabetically is considered "less than" the one that follows. So, the string that would come earlier in the dictionary is "less than". However, it's important to note that capital letters are considered "less than" lowercase letters because of their order in the Unicode system.
In this code, "apple" is less than "banana" because "apple" comes before "banana" in the dictionary.
Great work! In this lesson, you learned how to concatenate strings using both push_str and the + operator, check the length of strings, and perform string comparisons for equality and order. Understanding these methods and Rust's ownership model is crucial for efficient and safe string manipulation.
Now it's time to put your knowledge into practice through hands-on exercises. By practicing, you'll get a deeper understanding of how to effectively use these string methods and ownership concepts in Rust. Happy coding!
