Slices in Rust
Tags: rust
Published: 2023-02-05 13:33:51

In this short blog post, we'll explore about `Slice` in Rust

#### Slice

- A dynamically-sized view into a contiguous sequence. Contiguous here means that elements are laid out so that every element is the same distance from its neighbors
- Slices are a view into a block of memory represented as a pointer and a length
- Slices are either mutable or shared. The shared slice type is &T, while mutable slice type is &mut T, where T represents the element type
- As slices store the length of the sequence they refer to, they have twice the size of pointers to Sized types (i.e., types with a constant size known at compile time)

#### Slice Examples

In this below example, we're creating an integer array and then getting slice out of it

```
    // Declaring integer array
    let i_arr = [1, 2, 3, 4, 5, 6];

    // Getting slice array from main array 
    let i_slice_arr = &i_arr[..];

    // Using slice, iterating through all the elements of sliced integers
    i_slice_arr.iter().for_each(|x| print!("{x}, "));
```

It is possible get sub-set of an array as slice. We need to provide start and end index as below. In this example, we're getting elements from index 2 and 4.

```rust
    // it is possible to slice out sub-set of it
    i_slice_arr[2..5].iter().for_each(|x| print!("{x}, "));
```

If you want to get get element including end-index, we need to add `=` sign as below.

```rust
    // it is possible to slice out sub-set of it
    i_slice_arr[2..=5].iter().for_each(|x| print!("{x}, "));
```

It will be a panic, in case if the index goes out-of-range

```rust
    // it is possible to slice out sub-set of it
    i_slice_arr[2..=8].iter().for_each(|x| print!("{x}, "));
```

If we need to mutate the values of an array using slicing operation, we can get mutable iterator using `iter_mut` method as below. Prior to get mutable iterator, we must declare the underlying array as mutable using `mut`

```
    // Declaring integer array
    let mut i_arr = [1, 2, 3, 4, 5, 6];

    // Getting slice array from main array 
    let i_slice_arr = &mut i_arr[..];

    // Using slice, mutably iterating through the elements and change its values of the sliced integers
    // When we iterate mutably, underlying variable must be declared with mut keyword
    i_slice_arr.iter_mut().map(|x| *x * 2).for_each(|x| print!("{x}, "));
```

It is possible to have windows operation over slice. We can use `windows` or `chunks` method to iterate slice. In the below example, we use `chunks` method. On each iteration, we get a chunk size of 2 which is apparently a integer array

```rust
    i_slice_arr.chunks(2).for_each(|x| print!("{x:?}"));
```

Like Arrays, we can also slice over String. In the below example, we iterator over `str` slice, which is a char array, filter the elements that matches a given condition and then printing the same

```rust
    let s = String::from("Rust is awesome");
    let _ = &s[4..10].chars().filter(|c| c.is_ascii_lowercase()).for_each(|x| print!("{x}, "));
``` 

You can get the full source code from this [link](https://github.com/sivakumar-sivaprakasam/rust-examples/tree/main/slices)

Please feel free to share your comments if any

Happy reading!!!