Rust Strings Tutorial | String vs &str - Owned and Borrowed Types Explained
7views
1likes
09:13
T
Taught by Celeste AI - AI Coding Coach
View on YouTubeDescription
🦀 Master Rust Strings - Understanding Owned vs Borrowed String Types!
In this beginner-friendly tutorial, you'll learn the essential difference between String and &str in Rust. We'll write real code in nvim with syntax highlighting and see each example run in the terminal.
⏱️ What You'll Learn:
0:00 - Introduction
0:14 - String Basics: String vs &str
3:02 - String Methods: len, contains, replace, and more
5:49 - String Concatenation: 4 Ways to Combine Strings
8:54 - Recap & Next Steps
📚 Topics Covered:
• String::from() - Creating owned strings
• &str - String slices (borrowed references)
• String::new() - Creating empty strings
• push_str() - Appending text to strings
• len(), is_empty(), contains() - Common methods
• replace(), to_uppercase(), to_lowercase()
• trim() - Removing whitespace
• format!() macro - Flexible string formatting
• + operator - Concatenation (moves ownership)
• push() - Adding single characters
💻 Code Examples:
STRING BASICS:
fn main() {
let greeting = String::from("Hello, World!");
println!("Greeting: {}", greeting);
let name: &str = "Rust";
println!("Language: {}", name);
let mut message = String::new();
message.push_str("Learning ");
message.push_str("Rust!");
println!("Message: {}", message);
}
STRING METHODS:
fn main() {
let text = String::from("Hello Rust");
println!("Length: {}", text.len());
println!("Contains Rust: {}", text.contains("Rust"));
println!("Replaced: {}", text.replace("Rust", "World"));
println!("Uppercase: {}", text.to_uppercase());
}
STRING CONCATENATION:
fn main() {
// format! macro (most flexible)
let combined = format!("{} {}", "Hello", "World");
// push_str method
let mut greeting = String::from("Hi ");
greeting.push_str("there!");
// + operator (moves first string)
let c = String::from("Rust ") + "is fun!";
// push (single character)
let mut word = String::from("Hel");
word.push('l');
word.push('o');
}
🔑 Key Differences:
• String: Owned, growable, heap-allocated
• &str: Borrowed, immutable, string slice
• Use String when you need to own or modify
• Use &str for function parameters that only read
📌 Subscribe for more Rust tutorials!
#Rust #RustLang #Programming #LearnRust #CodingTutorial #Strings #BeginnerProgramming
Back to tutorialsOpen in YouTube
Duration
9:13
Published
December 21, 2025
Added to Codegiz
March 15, 2026