I'll try to explain what mistakes are in the code, and how it is done correctly.

##
1. Ranges

The slices in the for-loop use an `A..=B`

range, which is **inclusive**. This means that `&key[i ..= i+4]`

contains 5 elements: *i*, *i+1*, *i+2*, *i+3*, and *i+4*. Since you want only 4 elements, you need an `A..B`

range (without the equals sign). This means that A is included, but B is not.

```
let slice1 = &key[i .. i+4];
let slice2 = &key[i-4 .. i];
```

Alternatively, you can write `&key[i ..= i+3]`

to get a slice of the correct size.

##
2. Arrays vs. slices

An array `[T;N]`

has a constant size N, a slice `&[T]`

doesn't. Therefore, you can't use a slice where an array is required, so `xor(slice1,slice2)`

is forbidden. The easiest way to prevent this error is to use slices in `xor()`

, not arrays:

```
fn xor(tab1: &[usize], tab2: &[usize]) -> [usize; 4] {
let mut new_tab: [usize; 4] = [0, 0, 0, 0];
for n in 0 ..= 3 {
new_tab[n] = tab1[n] ^ tab2[n];
}
return new_tab;
}
```

##
3. Assigning a slice

Look at this line in the `main()`

function:

```
key[i .. i+4] = xor(slice1, slice2);
```

This is illegal: `key[i .. i+4]`

has the type `[usize]`

, so its size is unknown at compile-time. Therefore, slices always have to be behind a reference (e.g. `&[usize]`

).

But even if you add the `&`

, it won't compile, because you **can't assign to a slice** (this syntax is forbidden on the left side of an assignment). Instead, you can use `&[usize]::clone_from_slice()`

to clone the elements from a slice into another slice.

This is the corrected for-loop:

```
for i in SimpleStepRange(12, 0, -4) {
let i = i as usize;
let slice1 = &key[i .. i+4];
let slice2 = &key[i-4 .. i];
let result = xor(slice1, slice2);
&mut key[i .. i+4].clone_from_slice(&result[..]);
}
```

##
Performance

This implementation creates slices from an array, passes it to `xor()`

which then generates an array, which is then cloned into the original array – this is pretty inefficient.

##
Ideal implementation

The following implementation should be pretty efficient:

```
fn main() {
let mut data: [T; 32] = [
0xc6, 0x6a, 0xa6, 0x12, 0x4a, 0xba, 0x4d, 0x04, 0x4a, 0x22, 0x03,
0x54, 0x5b, 0x28, 0x0e, 0x63, 0xea, 0x79, 0x79, 0x20, 0xc8, 0x71,
0x44, 0x7d, 0x46, 0x62, 0x5f, 0x51, 0x85, 0xc1, 0x3b, 0xcb,
];
// Iterate over [12, 8, 4]
for i in SimpleStepRange(12, 0, -4) {
let i = i as usize;
// Get two mutable slices of the array
// split_at_mut() is needed to satisfy the borrow checker
let (slice2, slice1) = (&mut key[i-4 .. i+4]).split_at_mut(4);
// Perform xor on each element of the slices
for (a, b) in slice1.iter_mut().zip(slice2) {
*a = *a ^ *b;
}
}
}
```