Are associated constants real constants?


I've been playing around with moving some generic constants I use as config options inside of a trait, to make things cleaner. But associated constants don't seem to have the full range of capabilities of regular const and generic constants, except when used within the trait impl

For example:

trait ConfigTrait {
    const ARRAY_SIZE : usize;
struct Config();

impl ConfigTrait for Config {
    const ARRAY_SIZE : usize = 5;

fn do_struff<ConfigT : ConfigTrait>(config : ConfigT) {
    let _array = [0usize; ConfigT::ARRAY_SIZE];

fn main() {

When I hit some limitations, I'm used to the compiler telling me that there is an open issue to address this and I just need to be patient. For example, the compiler made me aware that I can't bound a generic type with an associated constant value, yet, because of Equality constraints on associated constants · Issue #70256 · rust-lang/rust · GitHub.

But not this time. Is this issue ("constant expression depends on a generic parameter") more fundamental?

Thank you

I think it's just more waiting. Unfortunately I don't have a good feel for how close one const feature is to stable versus another.

1 Like

Thanks! For some reason my version of the compiler didn't mention those RFCs, but the Playground's version of the compiler does. (perhaps because they're further along than the other RFCs, and already considered "unstable features"?)

Happy to know it's not a fundamental limitation that I couldn't wrap my head around.

Thanks again.

Yeah, I'm not sure why there's no feature-specific hint in this case. :man_shrugging:

Incidentally, sometimes there's a feature hint on nightly but not on stable, presumably because you can't use features on stable and not all features make it to stable. That's not the case here (nightly gives no additional hints), but when playing with new features like const, sometimes you can get clearer direction by trying on nightly instead of stable as a result.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.