Rust
December 2, 2026 6 min read

Slices in Rust

HS
HEMANT SINGH
Technical Writer & Educator

1. What is a Slice?

A slice is a reference to a continuous portion of a collection.

Instead of accessing the entire collection, we can access only the part we need.

For example, suppose we have an array:

let numbers = [10, 20, 30, 40, 50];

We can create a slice containing only:

20, 30, 40

Example

fn main() {
let numbers = [10, 20, 30, 40, 50];

let slice = &numbers[1..4];

println!("{:?}", slice);
}

Output:

[20, 30, 40]

Here:

  1. numbers is the original array.
  2. &numbers[1..4] creates a reference to part of the array.
  3. slice refers to the selected elements.

The original array is not copied.

2. Understanding Slice Syntax

The general syntax is:

&collection[start..end]

The starting index is included, but the ending index is excluded.

Example:

fn main() {
let numbers = [10, 20, 30, 40, 50];

let slice = &numbers[1..4];

println!("{:?}", slice);
}

Index positions:


Index

Value

0

10

1

20

2

30

3

40

4

50

The range 1..4 selects indexes 1, 2, and 3.

Therefore, the result is:

[20, 30, 40]

3. Different Ways to Create Slices

Rust provides different ways to specify slice ranges.

Consider this array:

let numbers = [10, 20, 30, 40, 50];

A. Select a Specific Range

let slice = &numbers[1..3];

Output:

[20, 30]

B. Start from the Beginning

You can omit the starting index.

let slice = &numbers[..3];

Output:

[10, 20, 30]

C. Continue Until the End

You can omit the ending index.

let slice = &numbers[2..];

Output:

[30, 40, 50]

D. Select the Entire Array

let slice = &numbers[..];

Output:

[10, 20, 30, 40, 50]

Quick Reference


Syntax

Meaning

&numbers[1..3]

Index 1 up to, but not including, 3

&numbers[..3]

Beginning up to index 3

&numbers[2..]

Index 2 to the end

&numbers[..]

Entire collection

4. What is the Slice Data Type?

For an array of integers, the slice type is &[i32].

Example:

fn main() {
let numbers = [10, 20, 30, 40];

let slice: &[i32] = &numbers[1..3];

println!("{:?}", slice);
}

Output:

[20, 30]

Here:

  1. & means a reference.
  2. [i32] means a slice of integers.
  3. &[i32] means a borrowed reference to an integer slice.

Unlike an array, a slice does not contain its own fixed length in its type. Its length is available at runtime.

5. Finding the Length of a Slice

Use the .len() method to find the number of elements in a slice.

Example:

fn main() {
let numbers = [10, 20, 30, 40, 50];

let slice = &numbers[1..4];

println!("Slice: {:?}", slice);
println!("Length: {}", slice.len());
}

Output:

Slice: [20, 30, 40]
Length: 3

6. Accessing Slice Elements

We can access individual slice elements using indexing.

Example:

fn main() {
let numbers = [10, 20, 30, 40, 50];

let slice = &numbers[1..4];

println!("First: {}", slice[0]);
println!("Second: {}", slice[1]);
println!("Third: {}", slice[2]);
}

Output:

First: 20
Second: 30
Third: 40

Remember: The index of a slice starts from zero, regardless of its position in the original array.

7. Mutable Slices

A mutable slice allows us to modify elements of the original collection.

Use &mut to create a mutable slice.

Example:

fn main() {
let mut numbers = [10, 20, 30, 40, 50];

let slice = &mut numbers[1..4];

slice[0] = 200;
slice[1] = 300;

println!("{:?}", numbers);
}

Output:

[10, 200, 300, 40, 50]

Here:

  1. The original array is declared using mut.
  2. &mut numbers[1..4] creates a mutable slice.
  3. Changes made through the slice affect the original array.

8. Using Slices with Functions

Slices are especially useful when passing collections to functions.

Instead of requiring a function to accept an array of a specific length, we can accept a slice.

Example: Display Array Elements

fn display(numbers: &[i32]) {
for number in numbers {
println!("{}", number);
}
}

fn main() {
let numbers = [10, 20, 30, 40, 50];

display(&numbers);
}

Output:

10
20
30
40
50

Here:

  1. &[i32] means the function accepts a borrowed integer slice.
  2. &numbers passes a reference to the entire array.
  3. The function can read the values without taking ownership.

Important Advantage

The same function can accept arrays of different lengths.

fn display(numbers: &[i32]) {
println!("{:?}", numbers);
}

fn main() {
let first = [10, 20, 30];
let second = [100, 200, 300, 400, 500];

display(&first);
display(&second);
}

Output:

[10, 20, 30]
[100, 200, 300, 400, 500]

This makes functions more flexible.

9. What is a String Slice?

A string slice is a reference to a portion of a string.

Its type is &str.

Example:

fn main() {
let name = String::from("Hello Rust");

let slice = &name[0..5];

println!("{}", slice);
}

Output:

Hello

Here:

  1. name is an owned String.
  2. &name[0..5] creates a string slice.
  3. slice refers to the first five bytes of the string.

10. Different Ways to Create String Slices

Consider this string:

let language = String::from("Rust Programming");

A. Specific Range

let slice = &language[0..4];

Output:

Rust

B. From the Beginning

let slice = &language[..4];

Output:

Rust

C. Until the End

let slice = &language[5..];

Output:

Programming

D. Entire String

let slice = &language[..];

Output:

Rust Programming

11. Important: UTF-8 String Slicing

Rust strings use UTF-8 encoding.

This means that not every character occupies exactly one byte.

For example:

fn main() {
let text = String::from("Hello");

let slice = &text[0..2];

println!("{}", slice);
}

Output:

He

This works because the selected byte positions are valid UTF-8 character boundaries.

However, slicing at an invalid character boundary can cause a runtime panic.

For example, this is unsafe:

fn main() {
let text = String::from("नमस्ते");

let slice = &text[0..1];

println!("{}", slice);
}

This can panic because byte index 1 is not a valid character boundary in this string.

Important: Use .chars() when you want to iterate over Unicode scalar values rather than working with byte positions.

12. String Slices vs String


Feature

String

String Slice (&str)

Ownership

Owns its text

Borrows text

Growable

Yes

No

Can modify text through it?

Yes, if mutable

No

Example

String::from("Rust")

&name[0..4]

Common use

Storing and modifying text

Reading or passing text

13. Returning a Slice from a Function

A function can return a string slice that refers to data owned by its caller.

Example:

fn first_word(text: &str) -> &str {
match text.find(' ') {
Some(index) => &text[..index],
None => text,
}
}

fn main() {
let sentence = String::from("Hello Rust Programming");

let word = first_word(&sentence);

println!("First Word: {}", word);
}

Output:

First Word: Hello

Here:

  1. find(' ') searches for the first space.
  2. Some(index) contains the position of the first space.
  3. &text[..index] returns the text before that space.
  4. If no space exists, the function returns the complete string slice.

The returned slice borrows from the input string. It does not own a separate string.

14. Difference Between Array, Slice and String Slice


Feature

Array

Array Slice

String Slice

Type example

[i32; 3]

&[i32]

&str

Stores

Fixed collection

Reference to part or all of an array

Reference to text

Element types

Same type

Same type

UTF-8 text

Size

Fixed

Length known at runtime

Length in bytes known at runtime

Example

[10, 20, 30]

&numbers[0..2]

&name[0..4]

15. Important Notes

  1. A slice is a reference to a continuous portion of a collection.
  2. Use & to create an immutable slice.
  3. Use &mut to create a mutable slice.
  4. Slice ranges include the starting index but exclude the ending index.
  5. Array slices use the type &[T].
  6. String slices use the type &str.
  7. Slices do not take ownership of the original collection.
  8. Array and string indexing starts from zero.
  9. String slices must use valid UTF-8 character boundaries.
  10. Use slices in function parameters when you want to accept collections of different lengths.

16. Quick Reference


Task

Syntax

Array slice

&numbers[1..4]

Slice from beginning

&numbers[..3]

Slice to end

&numbers[2..]

Entire array slice

&numbers[..]

Mutable array slice

&mut numbers[1..4]

Slice length

slice.len()

Array slice type

&[i32]

String slice

&name[0..4]

String slice type

&str

Iterate over slice

for item in slice