LibraryStrings

Strings

Learn about Strings as part of Rust Systems Programming

Understanding Strings in Rust

Strings are fundamental to programming, used for text manipulation, user input, and data representation. Rust offers two primary string types:

code
String
and
code
&str
(string slice). Understanding their differences and how to use them effectively is crucial for robust Rust development.

The `String` Type: Growable, Heap-Allocated

A

code
String
is a growable, mutable, owned, UTF-8 encoded string type. It's stored on the heap, meaning its size can change dynamically. This makes
code
String
ideal for situations where you need to build or modify strings.

`String` is Rust's owned, growable string type, stored on the heap.

Think of String like a dynamic array of characters that can expand as needed. You can add, remove, or modify its contents.

When you create a String using String::new() or String::from(), memory is allocated on the heap to hold the characters. This allows for efficient modification without needing to reallocate memory every time a single character is changed, as long as the total capacity is sufficient. When a String goes out of scope, its memory is automatically deallocated.

The `&str` Type: String Slices

A

code
&str
(pronounced "string slice") is a reference to a sequence of UTF-8 bytes. It's an immutable view into a string, often pointing to data stored elsewhere, such as string literals or parts of a
code
String
.
code
&str
is typically stored in the read-only data segment of the binary or on the stack.

`&str` is an immutable reference to a string, often a string literal.

A &str is like a pointer to a section of text. You can't change the text it points to, but you can use it to read and process that text.

String literals in Rust, like "hello", are of type &str. They are embedded directly into the program's binary. When you pass a &str to a function, you are passing a reference, which is efficient as it avoids copying the entire string data. Rust's ownership and borrowing rules ensure that &str references remain valid.

Converting Between `String` and `&str`

Rust makes it easy to convert between these two types. You can convert a

code
&str
to a
code
String
using methods like
code
to_string()
or
code
String::from()
. Converting a
code
String
to a
code
&str
is often done implicitly through borrowing, or explicitly using
code
&
.

FeatureString&str
OwnershipOwnedBorrowed
MutabilityMutableImmutable
StorageHeapStack/Read-only data
SizeGrowableFixed (view)
CreationString::new(), String::from()String literals ("...")

Common String Operations

Rust provides a rich set of methods for manipulating strings, including concatenation, searching, slicing, and iterating over characters.

What is the primary difference in storage between String and &str?

String is stored on the heap, while &str is typically stored on the stack or in the read-only data segment.

Concatenation can be done using the

code
+
operator (which consumes the left-hand
code
String
) or the
code
push_str()
method (which appends a
code
&str
without consuming the
code
String
). Slicing a
code
String
or
code
&str
creates a new
code
&str
referencing a portion of the original string.

Visualizing String Slicing: Imagine a String as a sequence of bytes. A string slice &str is like a pair of pointers: one to the start of the desired substring and one to the end. For example, if you have a String "Rust Programming" and you slice it from index 5 to 10 (exclusive), you get a &str that points to the characters "Progr". This operation doesn't copy the characters; it just creates a new reference to a specific segment of the original data.

📚

Text-based content

Library pages focus on text content

UTF-8 Encoding and Character Handling

Rust strings are guaranteed to be valid UTF-8. This means they can represent characters from virtually any language. Iterating over a string using

code
.chars()
yields
code
char
types, which represent Unicode scalar values. Be mindful that a single Unicode character might be represented by multiple bytes in UTF-8.

What encoding does Rust guarantee for its strings?

UTF-8

When performing byte-level operations or slicing, always ensure you are slicing on character boundaries to maintain UTF-8 validity. Incorrect slicing can lead to panics.

Learning Resources

The Rust Programming Language: Strings(documentation)

The official Rust book provides a comprehensive overview of `String` and `&str`, including their differences, common operations, and best practices.

Rust String Slices (`&str`) Explained(blog)

This tutorial dives deep into string slices, explaining their nature as references and how they are used in Rust programming.

Rust String Manipulation: A Practical Guide(blog)

A practical guide covering various string manipulation techniques in Rust, including concatenation, searching, and formatting.

Rust `String` vs `&str` - What's the Difference?(video)

A clear and concise video explanation highlighting the key distinctions between Rust's `String` and `&str` types.

Rust Standard Library: `String`(documentation)

The official API documentation for the `String` type, detailing all its available methods and associated functions.

Rust Standard Library: `str` (String Slice)(documentation)

The official API documentation for the `str` primitive type, outlining the methods available for string slices.

Understanding Rust's String Types(video)

This video provides a visual and conceptual explanation of how `String` and `&str` work in Rust, focusing on memory management.

Rust String Slicing Tutorial(video)

A tutorial specifically demonstrating how to perform slicing operations on Rust strings.

UTF-8(wikipedia)

Learn about the UTF-8 encoding standard, which is fundamental to understanding how Rust handles characters.

Rust Ownership and Borrowing(documentation)

A foundational chapter in the Rust book that explains ownership, borrowing, and lifetimes, which are critical for understanding string slices.