String vs. &str
Part of the Logic & Flow section of Coddy's Rust journey — lesson 54 of 66.
Rust has two main string types that serve different purposes: String and &str. Understanding the difference between them is crucial for working effectively with text in Rust.
A String is an owned, heap-allocated string that you can modify. When you create a String, your program owns that data and is responsible for managing its memory. You can grow it, shrink it, and change its contents because it's mutable when declared with mut.
let mut my_string = String::from("Hello");
my_string.push_str(" World"); // This works because String is owned and mutableIn contrast, &str (pronounced "string slice") is a borrowed, immutable view into string data. It doesn't own the data it points to - it's just a reference to some string content that exists elsewhere. String literals like "Hello" are actually of type &str.
let my_slice: &str = "Hello World"; // This is a string sliceThink of String as owning a book that you can write in and modify, while &str is like borrowing someone else's book - you can read it, but you can't change it. This distinction is fundamental to Rust's ownership system and helps ensure memory safety.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Cheat sheet
Rust has two main string types:
String- owned, heap-allocated, mutable string&str- borrowed, immutable string slice (reference to string data)
Creating a String:
let mut my_string = String::from("Hello");
my_string.push_str(" World");String literals are of type &str:
let my_slice: &str = "Hello World";String owns its data and can be modified, while &str is a reference that cannot be changed.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
The 'match' ExpressionMatching Multiple ValuesMatching RangesThe 'if let' ExpressionLoops as ExpressionsRecap - Simple Command Parser4Grouping Data with Structs
What is a Struct?Structs OverviewAccessing Struct FieldsMutable StructsStructs as Function ParametersTuple StructsRecap - Create a Book Struct7Handling Errors with 'Result'
What is a 'Result'?Using 'match' with 'Result'is_ok() and is_err()Shortcuts: unwrap and expectThe Question Mark Operator '?'Parsing Strings to NumbersRecap - Safe Division Function10Closures & Anonymous Functions
What is a Closure?Defining a Simple ClosureClosures with ParametersCapturing the EnvironmentRecap - Simple Adder Closure2Introduction to Vectors
What is a Vector?Creating a VectorAdding Elements with pushAccessing Vector ElementsIterating Over a VectorMutable IterationRemoving ElementsRecap - Basic Score Tracker5Key-Value Pairs with Hash Maps
What is a Hash Map?Creating a Hash MapInserting Key-Value PairsAccessing ValuesIterating Over a Hash MapUpdating a ValueRemoving a PairRecap - Word Counter8Project: Simple Item Inventory
Project SetupAdding an ItemChecking StockSelling an ItemPutting it all together6Handling Absence with 'Option'
What is an 'Option'?Using 'match' with 'Option'is_some() and is_none()Unwrapping an 'Option'The expectProviding a Default: unwrap_orRecap - Find an Element9String Slices and More
String vs. &strCreating String SlicesSlices as Function ParametersOther SlicesRecap - Find First Word