Exerpad
🔥1
10 XP
Lv.1 Beginner
Log In

Working with Strings

Text is everywhere in programs — names, messages, stories, and more! In Rust, working with text (called strings) is super important. Let's learn how!

Two Kinds of Strings

Rust has two main string types:

  • &str (called a "string slice") — a piece of text that can't be changed. When you write "hello" in your code, that's a &str.
  • String — a growable, changeable piece of text that lives on the heap.

Think of &str like a sign on a wall (you can read it but can't change it) and String like a whiteboard (you can write on it and erase it).

rust
fn main() {
let greeting: &str = "Hello!";
let name: String = String::from("Rustacean");
println!("{} {}", greeting, name);
}

You can turn a &str into a String two ways:

  • String::from("hello")
  • "hello".to_string()

Both do the same thing!

String Length and Checking Content

Use .len() to find out how many bytes a string has, and .contains() to check if a string includes some text:

rust
fn main() {
let message = String::from("I love Rust!");
println!("Length: {}", message.len());
println!("Has Rust? {}", message.contains("Rust"));
println!("Has Python? {}", message.contains("Python"));
}

Changing Case and Trimming

Rust gives you handy methods to change how text looks:

  • .to_uppercase() — makes everything SHOUTY
  • .to_lowercase() — makes everything quiet
  • .trim() — removes extra spaces from the beginning and end
rust
fn main() {
let shout = "hello world".to_uppercase();
println!("{}", shout);

let whisper = "HELLO WORLD".to_lowercase();
println!("{}", whisper);

let messy = " extra spaces ";
println!("'{}'", messy.trim());
}

Replacing Text

Use .replace() to swap one piece of text for another:

rust
fn main() {
let sentence = String::from("I like cats and cats like me");
let new_sentence = sentence.replace("cats", "dogs");
println!("{}", new_sentence);
}

Combining Strings with format!()

The best way to combine strings in Rust is with format!(). It works just like println!() but gives you back a new String instead of printing:

rust
fn main() {
let first = "Ferris";
let last = "the Crab";
let full = format!("{} {}", first, last);
println!("{}", full);
}

You can use + to combine strings, but it's tricky because + takes ownership of the first string. format!() is easier and safer!

Working with Characters

A string is made up of individual characters. Use .chars() to get each character one at a time:

rust
fn main() {
let word = "Rust";
for ch in word.chars() {
println!("{}", ch);
}
}

You can also grab a slice of a string using &s[start..end] (this works with byte positions):

rust
fn main() {
let word = String::from("Rustacean");
let first_four = &word[0..4];
println!("{}", first_four);
}

This prints Rust — the characters from position 0 up to (but not including) position 4.

Summary

MethodWhat it does
String::from("hi")Creates a String from text
"hi".to_string()Also creates a String from text
.len()Returns the length in bytes
.contains("x")Checks if a string contains text
.to_uppercase()Returns an ALL CAPS version
.to_lowercase()Returns an all lowercase version
.trim()Removes leading/trailing whitespace
.replace("a", "b")Replaces all "a" with "b"
format!("{} {}", a, b)Combines values into a new String
.chars()Iterates over each character
&s[0..4]Gets a slice of the string

Now let's practice working with strings!