`const` associated item not found?

Hello, I have trouble with understanding why the following code snippet won’t compile:

trait Bla {
    const SIZE: usize;
    fn blabla(&self);

struct A;

impl Bla for A {
    const SIZE: usize = 20;
    fn blabla(&self) {
        println!("blablabla {}", Self::SIZE)

struct B<T: Bla>{
    arr: [T; T::SIZE],

Can someone please explain why I am getting the following error?

error[E0599]: no associated item named `SIZE` found for type `T` in the current scope
  --> src/lib.rs:18:14
18 |     arr: [T; T::SIZE],
   |              ^^^^^^^ associated item not found in `T`
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `SIZE`, perhaps you need to implement it:
           candidate #1: `Bla`

As far as I understand, as long as T has Bla trait bound, I should be able to use T::SIZE, and since SIZE is declared as const, its value should be known at compile time and thus the size of the array (or am I wrong?).

Thank you.


The more explicit version [T; <T as Bla>::SIZE] now says “Bla is not implemented for T”. Looks strange to me, possibly the bounds are not “in scope” at that point…

I suggest to add a custom error message to rustc for this specific case, that says “it can’t be done for now” in very clear words.


To follow up @leonardo’s reply, there is no way to parameterize the length of an array until const generics arive.


Ah, too bad… Do you have any insight about when this might arrive on nightly? I noticed that the relevant tracking issue has been opened for more than a year.

I’m not sure, it is a huge addition and has been taking a while to arrive.

Last I heard, const generics should be ready (at least in nightly) some time in 2019, perhaps even the first half of the year. Not sure if anything’s changed in this regard in the last month or so.

See rust-lang/rust#43408, and maybe more importantly this summarizing quote from @dtolnay about why the error message is so bad:

According to #43408 (comment) the error message is “emergent” from the same reason we can’t “just” allow this to work right now. That means fixing the error message would be as hard as lifting the limitation in the first place, which is tracked in that issue.

1 Like