Python Slices & String Types (&str vs String)
A slice is a view into a contiguous sequence of elements without copying. Rust distinguishes between owned, growable heap strings (String) and borrowed read-only string slices (&str).
"A String is a full book you own on your bookshelf. A string slice (&str) is an index bookmark pointing to chapters 2 through 4 of that book—it doesn’t copy the pages, just references them."
Deep Dive: How It Works
String vs &str: String is a heap-allocated, growable buffer. &str is a pointer and length pointing to UTF-8 bytes (in the binary or on the heap).
Range Syntax: &s[0..5] borrows the first 5 bytes of string s.
Array Slices: &[i32] borrows a slice of an array or vector without allocation.
Syntax Blueprint
let s = String::from("hello world"); let hello: &str = &s[0..5];Slices store a starting memory pointer and a length, granting zero-copy sub-sequence views.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Slicing across multi-byte UTF-8 character boundaries.Why it happens: String slicing indexes raw bytes, not characters. Slicing in the middle of a multi-byte emoji causes a runtime panic.
How to fix: Ensure slice indices align with valid UTF-8 boundaries, or use .chars().
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Extract slice from a string
Given let greeting = String::from("Hello Rustaceans");
Extract the slice let rust = &greeting[6..16];
Print "Target: Rustaceans".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.