Cannot 'use' constant associated types.

f956853
Opened by Matthew Bettcher at 2021-06-08 12:28:14
struct Foo{}

impl Foo {
    const BAR: usize = 0;

    fn useConst(&mut self) {
        use Foo::BAR;
        println!("{:?}", BAR);    // cannot use just BAR, must use Foo::BAR
    }
}

Basically you should be able to 'use' an associated constant so you don't have to fully qualified names. In the above case once extra word is no big deal. But imagine you need to have 20 constants and you use them 100 times.

  1. cc @eddyb -- you are perhaps familiar with the discussions on this

    Mark Rousskov at 2017-09-18 17:46:09

  2. You can't use associated items. Maybe other @rust-lang/lang team members know where this was last discussed?

    Eduard-Mihai Burtescu at 2017-09-18 17:59:09

  3. I'd really like to see this feature. https://github.com/microsoft/windows-rs/issues/835 brought up some use cases for this as well as #86129

    Marcel Hellwig at 2021-06-08 09:45:18

  4. This is potentially implementable without heroic efforts, but with some limitations - https://internals.rust-lang.org/t/importing-associated-constants/6610/5 .

    Vadim Petrochenkov at 2021-06-08 12:28:14