# Allow importing an associated constant of a type?

**URL:** <https://internals.rust-lang.org/t/allow-importing-an-associated-constant-of-a-type/12559>\
**Category:** language design\
**Created:** [June 15, 2020, 6:48am UTC](https://internals.rust-lang.org/t/allow-importing-an-associated-constant-of-a-type/12559 "2020-06-15T06:48:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tesuji](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tesuji/32/12790_2.png) [@tesuji](https://internals.rust-lang.org/u/tesuji)\
**Post date:** [June 15, 2020, 6:48am UTC](https://internals.rust-lang.org/t/allow-importing-an-associated-constant-of-a-type/12559/1 "2020-06-15T06:48:04Z")

</div>

Currently Rust only allows importing items of a [module or crate](https://doc.rust-lang.org/nightly/reference/items/use-declarations.html#use-declarations).

```rust
pub use usize::MAX;
pub use std::primitive::usize::MAX as MAX2;

pub use Foo::MAX as MAX3;

pub struct Foo;
impl Foo {
    pub const MAX: usize = 42;
}

```

> **Run the above snippet give the following error:**
>
> ```rust
> error[E0432]: unresolved import `usize`
> --> src/lib.rs:1:9
> |
> 1 | pub use usize::MAX;
> | ^^^^^ help: a similar path exists: `std::usize`
> 
> error[E0432]: unresolved import `std::primitive::usize`
> --> src/lib.rs:2:25
> |
> 2 | pub use std::primitive::usize::MAX as MAX2;
> | ^^^^^ `usize` is a builtin type, not a module
> 
> error[E0432]: unresolved import `Foo`
> --> src/lib.rs:4:9
> |
> 4 | pub use Foo::MAX as MAX3;
> | ^^^ `Foo` is a struct, not a module
> 
> error: aborting due to 3 previous errors
> 
> For more information about this error, try `rustc --explain E0432`.
> 
> ```

It would allow simplifying this code in std:

> <https://github.com/rust-lang/rust/blob/4fb54ed484e2239a3e9eff3be17df00d2a162be3/src/libcore/num/f32.rs#L31-L32>

### Notes

- We've already allowed to re-import built-in types: [https://github.com/rust-lang/rust/blob/4fb54ed484e2239a3e9eff3be17df00d2a162be3/src/libcore/primitive.rs#L34-L35](https://github.com/rust-lang/rust/blob/4fb54ed484e2239a3e9eff3be17df00d2a162be3/src/libcore/primitive.rs#L34-L35)

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [June 15, 2020, 8:32am UTC](https://internals.rust-lang.org/t/allow-importing-an-associated-constant-of-a-type/12559/2 "2020-06-15T08:32:59Z")

</div>

> [@Importing associated constants?](https://internals.rust-lang.org/t/importing-associated-constants/6610):
>
> I have a code generator which is using structs with associated constants as an open-ended enum. For example: struct Thing(pub u32); impl Thing { const FOO: u32 = 0; const BAR: u32 = 1; //... } However, unlike an enum, I can’t do use Thing::\* to allow direct use of FOO/BAR/etc (or even an explicit use Thing::{FOO, BAR}). Is there some fundamental reason to not allow this, or is there already an RFC?

---

<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:** [September 13, 2020, 8:33am UTC](https://internals.rust-lang.org/t/allow-importing-an-associated-constant-of-a-type/12559/3 "2020-09-13T08:33:06Z")

</div>

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