# Pre²-RFC: Get an array's type/length at a type level

**URL:** <https://internals.rust-lang.org/t/pre-rfc-get-an-arrays-type-length-at-a-type-level/13613>\
**Category:** libs\
**Created:** [December 17, 2020, 5:43pm UTC](https://internals.rust-lang.org/t/pre-rfc-get-an-arrays-type-length-at-a-type-level/13613 "2020-12-17T17:43:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![whatisaphone](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/whatisaphone/32/6720_2.png) [@whatisaphone](https://internals.rust-lang.org/u/whatisaphone)\
**Post date:** [December 17, 2020, 5:43pm UTC](https://internals.rust-lang.org/t/pre-rfc-get-an-arrays-type-length-at-a-type-level/13613/1 "2020-12-17T17:43:49Z")

</div>

Let's say I have type `T` which is an array. It looks like there's no easy way (at a type level) to get the type or length. Would it make sense to have something like this in the stdlib:

```rust
impl<T, const N: usize> [T; N] {
    type Element = T;
    const LEN: usize = N;
}

```

It seems like this will be a common need when working with generic arrays. If you grep arrayvec's source for `::Item` or `::CAPACITY` (from their custom Array trait), you'll find them used dozens of times.

Today's real-world use case:

```rust
type PartialHash = [u8; 16];

fn partial_hash(data: &[u8]) -> PartialHash {
    // hardcode the length :(
    Sha256::digest(data)[..16].try_into().unwrap()
    // get the length from the type :)
    Sha256::digest(data)[..PartialHash::LEN].try_into().unwrap()
}

```

I'll bet the compiler can handle this already. Writing an extension trait works fine under min\_const\_generics: [Playground link](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=1c03c45d9975c4be1673e725af3e3543)

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [December 17, 2020, 6:05pm UTC](https://internals.rust-lang.org/t/pre-rfc-get-an-arrays-type-length-at-a-type-level/13613/2 "2020-12-17T18:05:54Z")

</div>

Unfortunately, normal impls can't have associated types, so you can't have `type Element`.

---

<div class="post-metadata">

**Author:** ![jschievink](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jschievink/32/3063_2.png) [@jschievink](https://internals.rust-lang.org/u/jschievink)\
**Post date:** [December 17, 2020, 8:43pm UTC](https://internals.rust-lang.org/t/pre-rfc-get-an-arrays-type-length-at-a-type-level/13613/3 "2020-12-17T20:43:35Z")

</div>

That restriction should just be lifted, I don't see any reason to forbid them there

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [December 17, 2020, 9:39pm UTC](https://internals.rust-lang.org/t/pre-rfc-get-an-arrays-type-length-at-a-type-level/13613/4 "2020-12-17T21:39:37Z")

</div>

Yes, I was just noting a current restriction. It should be lifted.

---

<div class="post-metadata">

**Author:** ![whatisaphone](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/whatisaphone/32/6720_2.png) [@whatisaphone](https://internals.rust-lang.org/u/whatisaphone)\
**Post date:** [December 18, 2020, 2:44pm UTC](https://internals.rust-lang.org/t/pre-rfc-get-an-arrays-type-length-at-a-type-level/13613/5 "2020-12-18T14:44:55Z")

</div>

Oh right, I forgot about that restriction. I guess we can revisit this sometime in the future.

This isn't a huge priority anyway, since you can DIY in userland

---

<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 18, 2021, 2:44pm UTC](https://internals.rust-lang.org/t/pre-rfc-get-an-arrays-type-length-at-a-type-level/13613/6 "2021-03-18T14:44:58Z")

</div>

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