# Use statement in implementation blocks

**URL:** <https://internals.rust-lang.org/t/use-statement-in-implementation-blocks/9561>\
**Category:** language design\
**Created:** [March 6, 2019, 4:02pm UTC](https://internals.rust-lang.org/t/use-statement-in-implementation-blocks/9561 "2019-03-06T16:02:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyras](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kyras/32/5426_2.png) [@Kyras](https://internals.rust-lang.org/u/Kyras)\
**Post date:** [March 6, 2019, 4:02pm UTC](https://internals.rust-lang.org/t/use-statement-in-implementation-blocks/9561/1 "2019-03-06T16:02:58Z")

</div>

Hello, I recently discussed on the subreddit[1], why is it not possible to have an `use` statement inside of the `impl` block, which would be extremely handy, mainly in enumeration implementations.

For clearance, effect of the use statement would be inside of the implementation block, same as is with the mod block, so imported names would be visible in all methods defined in the implementation block, without contaminating whole module, in which the enumeration is defined:

```rust
enum List<T> {
  Cons {
    value: T,
    next: Box<List<T>>,
  },
  Nil,
}

impl<T> List<T> {
  use crate::LinkedList::*;
  
  pub fn empty() -> Self {
    Nil
  }
}

```

Thanks & Cheers !

[1] [https://www.reddit.com/r/rust/comments/axzcmb/why\_is\_use\_statement\_not\_allowed\_in\_impl\_block/](https://www.reddit.com/r/rust/comments/axzcmb/why_is_use_statement_not_allowed_in_impl_block/)

---

<div class="post-metadata">

**Author:** ![Kyras](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kyras/32/5426_2.png) [@Kyras](https://internals.rust-lang.org/u/Kyras)\
**Post date:** [March 6, 2019, 8:18pm UTC](https://internals.rust-lang.org/t/use-statement-in-implementation-blocks/9561/2 "2019-03-06T20:18:01Z")

</div>

My apologies, just found out there was an RFC which tried to deal with this, but was ultimately rejected, because of deeper internal problems. There is nice explenation in the subreddit.

Cheers !

---

<div class="post-metadata">

**Author:** ![fotocoyotl](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fotocoyotl/32/5194_2.png) [@fotocoyotl](https://internals.rust-lang.org/u/fotocoyotl)\
**Post date:** [March 6, 2019, 8:19pm UTC](https://internals.rust-lang.org/t/use-statement-in-implementation-blocks/9561/3 "2019-03-06T20:19:15Z")

</div>

In the case you really do need to `use` things at the `impl` level, you can move that `use` statement inside of the `fn` and it should work.

---

<div class="post-metadata">

**Author:** ![Kyras](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kyras/32/5426_2.png) [@Kyras](https://internals.rust-lang.org/u/Kyras)\
**Post date:** [March 6, 2019, 8:31pm UTC](https://internals.rust-lang.org/t/use-statement-in-implementation-blocks/9561/4 "2019-03-06T20:31:42Z")

</div>

Yes, I am aware, but it would be cool, if you would not had to copy&paste that use statement into all methods defined inside the `impl`, but rather define single `use` at the top of `impl`. Other solution would be putting it into the module, where the `impl` block is defined, but that would contaminate the whole block with that.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [March 7, 2019, 1:18am UTC](https://internals.rust-lang.org/t/use-statement-in-implementation-blocks/9561/5 "2019-03-07T01:18:17Z")

</div>

Because it doesn’t matter where you put an impl, you can also just do

```rust
mod whatever_it_does_not_matter {
  use super::*;
  use crate::LinkedList::*;
  impl<T> List<T> {  
    pub fn empty() -> Self {
      Nil
    }
  }
}

```

---

<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:** [June 5, 2019, 1:18am UTC](https://internals.rust-lang.org/t/use-statement-in-implementation-blocks/9561/6 "2019-06-05T01:18:18Z")

</div>

Closed on June 5, 2019, 1:18am UTC

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