Strings in Rust
In this lesson, you will learn how strings work in Rust. Strings are one of the most commonly used data types and are essential for handling text.
Rust provides two main types for working with text: string literals and the String type.
Understanding the difference between them is very important.
What Is a String?
A string is a sequence of characters used to represent text.
In Rust, strings are UTF-8 encoded, which means they can safely store Unicode characters from many languages.
String Literals (&str)
A string literal is a reference to a fixed string stored directly in the program’s binary.
String literals are written inside double quotes and are immutable.
fn main() {
let language = "Rust";
println!("{}", language);
}
Here, language is a string slice of type &str.
The String Type
The String type is a growable, mutable string stored on the heap.
Unlike string literals, String values can be modified.
fn main() {
let mut name = String::from("Rust");
name.push_str(" Programming");
println!("{}", name);
}
The String::from() function creates a new String value.
Why Rust Has Two String Types
Rust separates string literals and String to provide both:
- High performance for fixed text
- Flexibility for dynamic text
This design allows Rust to manage memory safely and efficiently.
Creating a String
There are multiple ways to create a String.
fn main() {
let s1 = String::new();
let s2 = String::from("Hello");
let s3 = "World".to_string();
println!("{}", s2);
println!("{}", s3);
}
All three methods create a String value.
Updating Strings
Strings can be updated using methods like push and push_str.
fn main() {
let mut text = String::from("Hello");
text.push(' ');
text.push_str("Rust");
println!("{}", text);
}
The push method adds a single character,
while push_str adds a string slice.
Concatenating Strings
Rust allows string concatenation using the + operator or the format! macro.
Using +
fn main() {
let s1 = String::from("Hello");
let s2 = String::from("Rust");
let result = s1 + " " + &s2;
println!("{}", result);
}
Note that s1 is moved and can no longer be used after concatenation.
Using format!
The format! macro is often preferred because it does not take ownership of values.
fn main() {
let s1 = "Hello";
let s2 = "Rust";
let message = format!("{} {}", s1, s2);
println!("{}", message);
}
String Length
The length of a string can be obtained using the len() method.
fn main() {
let text = String::from("Rust");
println!("{}", text.len());
}
The length is measured in bytes, not characters.
Iterating Over Strings
Rust allows iteration over characters using the chars() method.
fn main() {
let word = "Rust";
for c in word.chars() {
println!("{}", c);
}
}
📝 Practice Exercises
Exercise 1
Create a string literal and print it.
Exercise 2
Create a mutable String and append text to it.
Exercise 3
Concatenate two strings using the format! macro.
✅ Practice Answers
Answer 1
fn main() {
let text = "Learning Rust";
println!("{}", text);
}
Answer 2
fn main() {
let mut s = String::from("Hello");
s.push_str(" World");
println!("{}", s);
}
Answer 3
fn main() {
let a = "Rust";
let b = "Language";
let result = format!("{} {}", a, b);
println!("{}", result);
}
What’s Next?
Now that you understand how strings work in Rust, you are ready to learn about tuples and arrays.
In the next lesson, you will explore Rust’s tuple and array data structures.