Const slicing

I think you'd run into a couple problems...

  1. The std::ops::Index trait expects you to return a reference and not a value, so you'd need to either rely on [T; FIBS.len() - 1] being Copy or use something else
  2. You need to move the runtime value, 1.. (a Range<usize>) to something compile time
  3. We need to use unsafe and pointer arithmetic in a const fn
  4. Const generics need to be able to express conditions like N < M in a where clause

Here's my take:

struct Array<T, const N: usize>([T; N]);

struct Range<const Start: usize>;

impl<T, const N: usize> Array<T, N> {
    pub const fn slice<const Start: usize>(self, range: Range<Start>) -> [T; N - Start]
    where
        Start <= N,
    {
        unimplemented!("Magic...")
    }
}
2 Likes