Slices

Rust is a systems programming language focused on speed, memory safety, and fearless concurrency — and slices let you reference part of a collection without copying it.

Learn Slices in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll create string slices ( &str ) and array slices ( &[T] ), and write functions that return borrowed views into data.

What You'll Learn in This Lesson

1️⃣ String Slices

A string slice , written &str , references a portion of a String . You create one with a range: &s[0..5] borrows the first five bytes. The end index is exclusive, and you can drop the 0 or the end for handy shorthands.

No data was copied here — each slice is a lightweight reference into the original s . That's why slices are so cheap to pass around.

2️⃣ Slices in Functions and on Arrays

Slices shine as function parameters and return types. A function returning the first word can hand back a &str that borrows the input — no allocation. The same idea applies to arrays: &[i32] is a slice of integers, letting one function work on a whole array or just part of it.

Because first_word takes &str , it accepts a String , a literal, or another slice. And sum works on any slice of i32 , whether from an array or a vector.

Your turn. Fill in the blanks marked ___ , then run it.

Write a last_word function that returns a slice of the final word. Run it with cargo run .

📋 Quick Reference — Slices

Practice quiz

What is a slice in Rust?

  • A copy of a collection
  • A new heap allocation
  • A reference to a contiguous run of elements
  • A type of loop

Answer: A reference to a contiguous run of elements. A slice borrows a contiguous view into existing data without copying.

What type is a string slice written as?

  • &str
  • String
  • char
  • Vec<u8>

Answer: &str. A string slice has type &str.

Given s = "hello world", what is &s[0..5]?

  • world
  • hello world
  • ello
  • hello

Answer: hello. Bytes 0 through 4 form "hello"; the end index is exclusive.

Is the end index of a slice range inclusive or exclusive?

  • Inclusive
  • Exclusive
  • Depends on the type
  • It is the length

Answer: Exclusive. The end of a slice range is exclusive, so &s[0..5] excludes index 5.

What does the shorthand &s[..] produce?

  • The entire string
  • An empty slice
  • The first char
  • An error

Answer: The entire string. Omitting both ends slices the whole thing.

Why should functions take &str instead of &String?

  • It is faster to type
  • &String is invalid
  • &str accepts literals, slices, and Strings
  • It avoids the heap

Answer: &str accepts literals, slices, and Strings. &str is more general; a String coerces to &str automatically.

What is the type of a slice of i32 values?

An array slice of integers is &[i32].

Does creating a slice copy the underlying data?

  • Yes, always
  • Only for strings
  • Only for arrays
  • No, it just borrows a view

Answer: No, it just borrows a view. A slice is a pointer plus length; it borrows without copying.

What does &nums[1..4] give for nums = [10,20,30,40,50]?

Indices 1, 2, 3 (end exclusive) are 20, 30, 40.

What happens if you slice through the middle of a multi-byte UTF-8 character?

  • It rounds down
  • It returns None
  • The program panics
  • Nothing

Answer: The program panics. String slices must land on char boundaries or the program panics.