Coding/Learn/rust/slices and strings
Rust Foundational Course
01.Introduction to Rust & Memory Safety
02.Formatting, Comments & Macros (println!)
03.Variables, Mutability & Shadowing
04.Scalar & Compound Types (Integers, Floats, Tuples, Arrays)
05.Functions, Parameters & Return Expressions
06.Conditionals & If as an Expression
07.Ownership Rules & Move Semantics
08.References & Borrowing (&T)
09.Mutable References (&mut) & Aliasing XOR Mutability
10.Slices & String Types (&str vs String)
11.Loops in Rust (loop, while, for)
12.Pattern Matching with match Expressions
13.Structs, Associated Functions & Methods (impl)
14.Enums & the Option<T> Type (No Null!)
15.Concise Pattern Matching (if let & let else)
16.Vectors & Growable Collections (Vec<T>)
17.Error Handling with Result<T, E> & the ? Operator
18.Traits, Shared Behavior & Generics
19.Closures & Functional Iterators (map, filter, collect)
20.Capstone: High-Performance In-Memory Log Telemetry Engine
Course Certificate0%

Complete all lessons to earn

LearnSlices & String Types (&str vs String)
10 / 20 0%
Lesson 10 of 20
6 mins read

Slices & String Types (&str vs String)

PreviousNext Lesson
In Plain English

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).

The Mental Model

"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.

PointCore Rules to Remember

Point star
&str: Zero-cost borrowed view into UTF-8 text.
Point star
String: Owned, dynamically growable heap-allocated string buffer.
Point star
Slice Ranges: &arr[1..4] views indices 1, 2, and 3 without copying.

Common Beginner Traps & How to Fix Them

Mistake: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().

Pro Tip: In function arguments, always prefer &str over &String because &String coerces into &str automatically (deref coercion).

Live Interactive Example

Hit Run Code to see it live
String and Array Slicing
Rust 1.78 / rustc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates zero-copy string and array slicing.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Extract slice from a string

  • PointGiven let greeting = String::from("Hello Rustaceans");
  • PointExtract the slice let rust = &greeting[6..16];
  • PointPrint "Target: Rustaceans".
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates