10

I have an &[u8] and would like to turn it into an &[u8; 3] without copying. It should reference the original array. How can I do this?

Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
SoniEx2
  • 1,694
  • 2
  • 21
  • 38

3 Answers3

24

As of Rust 1.34, you can use TryFrom / TryInto:

use std::convert::TryFrom;

fn example(slice: &[u8]) {
    let array = <&[u8; 3]>::try_from(slice);
    println!("{:?}", array);
}

fn example_mut(slice: &mut [u8]) {
    let array = <&mut [u8; 3]>::try_from(slice);
    println!("{:?}", array);
}
Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
  • 1
    If you're passing it into a function that expects such a parameter, you can also use `try_into().unwrap()` to avoid the need to write out the array type, and if you're sure the slice is large enough. – Stephen Chung Dec 23 '19 at 10:37
9

Just to re-emphasize, this can't be done without unsafe code because you don't know until runtime that the slice has three elements in it.

fn slice_to_arr3<T>(slice: &[T]) -> Option<&[T; 3]> {
    if slice.len() == 3 {
        Some(unsafe { &*(slice as *const [T] as *const [T; 3]) })
    } else {
        None
    }
}

This can't be generic over the length of the array until const generics are implemented.

durka42
  • 1,401
  • 10
  • 16
  • 5
    I'd quibble over the phrasing "this can't be safe". It is most definitely *safe* — as long as you check the length of the slice. If it actually could never be safe, then it should never be written in the first place. It's a pedantic argument but the concept of "safety" is muddled enough for Rust newcomers that I find it worth pointing out. – Shepmaster Jan 06 '18 at 17:28
  • He might have meant "this can't be non-unsafe". – mcarton Jan 06 '18 at 19:36
  • 1
    Yes I agree. Maybe better phrasing is _cannot be done without unsafe code_. – durka42 Jan 06 '18 at 20:42
  • 1
    Nothing can be "done without unsafe code" by that logic since the stdlib necessarily needs unsafe code, everything one shall ever use internally has that. I think the correct phrasing is "this problem has no total solution"; any solution to it has to be partial and account for the error case where the slice does not have at least 3 elements, somehow. – Zorf Jan 10 '20 at 21:14
  • 2
    @Zorf the phrasing I like to use is to draw a difference between *safe* and *sound*. The code is not safe because you have to use the `unsafe` keyword, no way around that. But it is sound because we check the length of the slice, as the compiler is trusting us to do. – durka42 Jan 11 '20 at 18:43
  • It definitely can be done in safe code; see Shepmaster's answer. It could also have been done in safe code prior to Rust 1.34, with a loop over the elements or with [copy_from_slice](https://doc.rust-lang.org/std/primitive.slice.html#method.copy_from_slice). – Daira Hopwood Aug 23 '21 at 15:58
9

They arrayref crate implements this.

Here's an example, you can of course use it in different ways:

#[macro_use]
extern crate arrayref;

/// Get the first 3 elements of `bytes` as a reference to an array
/// **Panics** if `bytes` is too short.
fn first3(bytes: &[u8]) -> &[u8; 3] {
     array_ref![bytes, 0, 3]
}
bluss
  • 10,836
  • 42
  • 47