Introduction
The Slice
class in Rust represents a dynamically-sized view into a contiguous sequence of elements. Slices are used to borrow sections of arrays, vectors, or other sliceable collections, allowing you to work with a portion of a collection without copying the data.
Using Slice
The Slice
class can be used to create views into arrays or vectors. Here is a basic example:
fn main() {
let arr = [1, 2, 3, 4, 5];
let slice = &arr[1..4];
println!("Slice: {:?}", slice);
}
// Output: Slice: [2, 3, 4]
Key Methods
Below are some of the key methods exposed by the Slice
class:
len
Returns the number of elements in the slice.
fn main() {
let arr = [1, 2, 3, 4, 5];
let slice = &arr[1..4];
println!("Length: {}", slice.len());
}
// Output: Length: 3
is_empty
Returns true
if the slice is empty.
fn main() {
let arr: [i32; 0] = [];
let slice = &arr[..];
println!("Is empty: {}", slice.is_empty());
}
// Output: Is empty: true
get
Returns an option containing a reference to the element at the specified index, or None
if the index is out of bounds.
fn main() {
let arr = [1, 2, 3, 4, 5];
let slice = &arr[1..4];
match slice.get(1) {
Some(&val) => println!("Value at index 1: {}", val),
None => println!("No value at index 1"),
}
}
// Output: Value at index 1: 3
first / last
Returns an option containing a reference to the first / last element, or None
if the slice is empty.
fn main() {
let arr = [1, 2, 3, 4, 5];
let slice = &arr[1..4];
println!("First: {:?}", slice.first());
println!("Last: {:?}", slice.last());
}
// Output:
// First: Some(2)
// Last: Some(4)
iter
Returns an iterator over the slice's elements.
fn main() {
let arr = [1, 2, 3, 4, 5];
let slice = &arr[1..4];
for val in slice.iter() {
println!("{}", val);
}
}
// Output:
// 2
// 3
// 4
Example Usage
Example 1: Basic Usage
fn main() {
let arr = [1, 2, 3, 4, 5];
let slice = &arr[1..4];
println!("Slice: {:?}", slice);
}
// Output: Slice: [2, 3, 4]
Example 2: Getting Slice Length
fn main() {
let arr = [1, 2, 3, 4, 5];
let slice = &arr[1..4];
println!("Length: {}", slice.len());
}
// Output: Length: 3
Example 3: Checking if Slice is Empty
fn main() {
let arr: [i32; 0] = [];
let slice = &arr[..];
println!("Is empty: {}", slice.is_empty());
}
// Output: Is empty: true
Example 4: Accessing Elements Safely
fn main() {
let arr = [1, 2, 3, 4, 5];
let slice = &arr[1..4];
match slice.get(1) {
Some(&val) => println!("Value at index 1: {}", val),
None => println!("No value at index 1"),
}
}
// Output: Value at index 1: 3
Example 5: Iterating Over Elements
fn main() {
let arr = [1, 2, 3, 4, 5];
let slice = &arr[1..4];
for val in slice.iter() {
println!("{}", val);
}
}
// Output:
// 2
// 3
// 4
Considerations
- Slices are a view into an existing collection and do not own the data they reference.
- Slicing an array or vector is done using the
&[start..end]
syntax, wherestart
is inclusive andend
is exclusive. - Be careful with slice indices to avoid out-of-bounds errors, which will cause a panic at runtime.
- Slices can be used to borrow parts of a collection immutably or mutably.