9951 explained code solutions for 126 technologies


rustHow to display a Rust slice?


A Rust slice is a data structure that allows access to a contiguous sequence of elements in a collection. It is a reference to a contiguous region of memory and can be used to view the underlying data in the collection.

Example code

let numbers = [1, 2, 3, 4, 5];
let slice = &numbers[1..3];
println!("{:?}", slice);

Output example

[2, 3]

Code explanation

  • let numbers = [1, 2, 3, 4, 5];: This line creates an array of numbers.
  • let slice = &numbers[1..3];: This line creates a slice of the array, starting at index 1 and ending at index 3.
  • println!("{:?}", slice);: This line prints the slice to the console.

Helpful links

Edit this code on GitHub