# Pre-RFC: Constant slicing

**URL:** <https://internals.rust-lang.org/t/pre-rfc-constant-slicing/5249>\
**Category:** language design\
**Created:** [May 17, 2017, 5:56am UTC](https://internals.rust-lang.org/t/pre-rfc-constant-slicing/5249 "2017-05-17T05:56:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [May 17, 2017, 5:56am UTC](https://internals.rust-lang.org/t/pre-rfc-constant-slicing/5249/1 "2017-05-17T05:56:50Z")

</div>

# Problem statement

Currently the standard practice for code is to use slices as input arguments, even if it’s known that function must accept only fixed length input. One of the reasons for it is references to arrays (i.e. [T; 5]) can feel the second order citizens compared to slices, due to the traits being implemented only for sizes up to 32. This should be soon solved by this [RFC](https://github.com/rust-lang/rfcs/pull/2000). But other reason is that taking array references from an existing buffer is unergonomic and simply not possible in safe Rust.

Because of it existing code often omits important information from the API type signature, which can have negative impact on the performance and soundness of the code.

#Proposal I would like to propose to add the second slicing type, the constant one. Essentially it will be a syntactic sugar to [`arrayref`] ([https://crates.io/crates/arrayref](https://crates.io/crates/arrayref)) crate macro and can look like `&foo[N:M]`, where `N` and `M` required to be `usize` constants (generic or `{integer}`), and `&foo` must provide `Deref`s for `[T]`. As a result this expression will return `&[T; M-N]`, if `M > foo.len()` or `N > M` it will panic. `&foo[:M]` will be equivalent to `N = 0` and `&foo[N:]` will be allowed only if `foo` is an array, so `M` can be taken as `foo.len()`.

Syntax can be changed to something else to avoid possible ambiguity.

#Alternatives Leave everything as is. Teach about `arrayref` crate.

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [May 17, 2017, 7:50am UTC](https://internals.rust-lang.org/t/pre-rfc-constant-slicing/5249/2 "2017-05-17T07:50:40Z")

</div>

This isn’t really possible yet without compiler magic or type level integers.

RFC issues [https://github.com/rust-lang/rfcs/issues/1772](https://github.com/rust-lang/rfcs/issues/1772) and [https://github.com/rust-lang/rfcs/issues/1833](https://github.com/rust-lang/rfcs/issues/1833) talk about this feature with some more detail on possible implementations.

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [May 17, 2017, 4:13pm UTC](https://internals.rust-lang.org/t/pre-rfc-constant-slicing/5249/3 "2017-05-17T16:13:09Z")

</div>

Ah, couldn’t find those issues while searching for something relevant.

It would be definitely cool to implement “value refinement” approach from the first issue, but I am not sure if it’s possible to do it in a backward compatible way. Thus proposal for a new syntax.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:28am UTC](https://internals.rust-lang.org/t/pre-rfc-constant-slicing/5249/4 "2019-03-25T08:28:33Z")

</div>

Closed on March 25, 2019, 8:28am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
