Strings
Strings in Rust diverge a little bit from conventional programming languages. The approach to string manipulation in Rust is influenced by its system-focused design. There are two types of strings that differ in various aspects such as modification, performance of operations and ownership rules.
Tackling data structures of variable size, such as strings, can get a bit tricky and Rust has its own spin on it. In Rust, a string is essentially a sequence of Unicode characters encoded in UTF-8. For example, each character in the “Codecademy Rust concept,” string is a valid Unicode entity, i.e., “C,” “o,” “d,” “e,” “c,” and so on.
String types
In Rust, there are two main types related to strings, each serving a specific purpose.
String
- Description: A heap-allocated, growable, and mutable string type.
- Usage: Used when dynamic string manipulation and ownership transfer are required.
let mut dynamic_string = String::from("Hello, Rust!");
&str
(String Slice)
- Description: A reference to a sequence of UTF-8 bytes, often used as a view into a
String
or a string literal. - Usage: Commonly used for string references without ownership or when dealing with parts of a string.
let string_literal: &str = "Hello, Rust!";
Note: These string types cover various scenarios, from dynamic and mutable strings
String
to static and immutable string slices&str
. The type of string should be chosen depending on the specific requirements and the characteristics of the data being manipulated.
Creating Strings
Here is an example of creating a string using String::new()
:
let mut empty_string = String::new();
String Manipulation
Concatenation
In Rust, there are multiple ways to concatenate strings. Here is a list of concatenation operators including examples:
+
Operator:
- Usage: Concatenating two strings.
- Ownership: Consumes the left operand.
- Borrowing: To concatenate with a borrowed string (sliced out of another string using
&str
), it is necessary to explicitly borrow it using the&
operator.
let hello = String::from("Hello, ");let world = String::from("World!");let hello_world = hello + &world; // Ownership of 'hello' is moved, 'world' is borrowed
+=
Operator:
- Usage: In-place concatenation operation.
- Ownership: Consumes the existing string on the left operand and appends to it.
- Borrowing: It works directly with the mutable reference of the left operand, but the ownership of the left operand is transferred to the result.
let mut hello = String::from("Hello, ");let world = String::from("World!");hello += &world; // Appends 'World!' to the existing 'Hello, '
format!
Macro:
- Usage: Creates a new string by formatting text, allowing string interpolation.
- Ownership: It does not involve ownership transfer and is a convenient way to create strings without borrowing or ownership concerns.
let hello = String::from("Hello, ");let world = String::from("World!");let hello_world = format!("{}{}", hello, world); // Creates a new string without ownership issues
Note: When working with strings in Rust, it’s crucial to be mindful of ownership and borrowing semantics, especially when using operators like
+
. The operator+
is used to create a new string while+=
is used to modify an existing string in place. To concatenate strings with interpolation theformat!
macro provides a flexible and ownership-friendly way.
Slicing & Appending with push_str
and push
let mut message = String::from("Rust");message.push_str(" Programming");message.push('!');let part_of_message = &message[0..5];
In the example above, push_str
is used to append a string slice to the existing string message
, and push
is used to add the character !
to the end of the concatenated string.
Referencing Strings
To reference parts of a string without ownership &str
can be used as follows:
let full_string = String::from("Hello, World!");let slice = &full_string[0..5];
Here, &full_string[0..5]
creates a reference to the first five characters of full_string
.
A certain familiarity with the described methods and operators as well as the concept of string slices is crucial to work efficiently with and fully grasp the syntax of Rust strings.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.