# Trait importing syntax

**URL:** <https://internals.rust-lang.org/t/trait-importing-syntax/1209>\
**Category:** bikeshed (deprecated)\
**Created:** [January 6, 2015, 10:02pm UTC](https://internals.rust-lang.org/t/trait-importing-syntax/1209 "2015-01-06T22:02:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Screwtapello](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/screwtapello/32/1076_2.png) [@Screwtapello](https://internals.rust-lang.org/u/Screwtapello)\
**Post date:** [January 6, 2015, 10:02pm UTC](https://internals.rust-lang.org/t/trait-importing-syntax/1209/1 "2015-01-06T22:02:55Z")

</div>

Most of the time, I’m a Python programmer, but sometimes I want to do something that requires compiled performance or a standalone executable or something, and I’m really interested in Rust as a complement to Python in my toolbox.

One of my favourite things about Python is that, if you’re following the style-guide, then every identifier in a source file is either defined in that file, or specifically imported at the top. Conversely, if you see a name imported at the top of the file and it’s not mentioned anywhere else, that means it’s an unused import and you can remove it.

In Rust, as far as I can tell this is _almost_ true: it’s true for importing modules, but not for activating traits, which are lexically identical to module imports:

```
use a::Foo;
use b::Bar;
use c::Baz;
use d::Qux;

```

Quick, which of those is an unused import, and which is a trait activation that’s vitally important to the running of the code?

It’s probably too late to change this now, but I wish the trait activation syntax were visibly different from importing. For example, in the same way that you have `mod foo { ... }` to define a module and `mod foo;` to pin it in place, maybe you could have a trait declaration:

```
use a::Foo;
use b::Bar;
trait c::Baz;
use d::Qux;

```

Alternatively, you could split the two uses of `use` with a modifying keyword:

```
use mod a::Foo;
use mod b::Bar;
use trait c::Baz;
use d::Qux; // this is a struct, not a module!

```

For backwards compatibility you could say that without `mod` or `trait`, `use` guesses what kind of thing you’re importing, but let the style-guide encourage people not to make the computer guess.

(For completeness, it can also be confusing if you see code calling `foo.bar()` and `foo` does not implement a `bar()` method. Obviously a trait is involved somewhere, but figuring out which one can be tedious. I can’t think of a way to make it _not_ tedious without destroying the pleasant conciseness of traits, but at least if trait-imports were easy to spot, it would narrow the search space.)

---

<div class="post-metadata">

**Author:** ![tbu](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tbu](https://internals.rust-lang.org/u/tbu)\
**Post date:** [January 6, 2015, 10:23pm UTC](https://internals.rust-lang.org/t/trait-importing-syntax/1209/2 "2015-01-06T22:23:31Z")

</div>

The good thing is that there is a `unusued_imports` lint, so you’ll at least get notified if you import too much.

---

<div class="post-metadata">

**Author:** ![d3zd3z](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/d3zd3z/32/511_2.png) [@d3zd3z](https://internals.rust-lang.org/u/d3zd3z)\
**Post date:** [January 6, 2015, 11:23pm UTC](https://internals.rust-lang.org/t/trait-importing-syntax/1209/3 "2015-01-06T23:23:00Z")

</div>

With the module conventions, the module names should start with a lowercase identifier, so should be pretty distinct. There isn’t, however, a distinction made between types and traits.

So, in your example above, it should be:

```
use a::foo;
use b::bar;
use c::Baz;
use d::Qux;

```

and the only thing not easy to tell are which ones are traits and which ones are structs. Does this still seem worthwhile to distinguish via syntax?

---

<div class="post-metadata">

**Author:** ![mzabaluev](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mzabaluev/32/3082_2.png) [@mzabaluev](https://internals.rust-lang.org/u/mzabaluev)\
**Post date:** [January 7, 2015, 11:11am UTC](https://internals.rust-lang.org/t/trait-importing-syntax/1209/4 "2015-01-07T11:11:21Z")

</div>

I started a thread some time back with a proposal to mitigate this: [http://discuss.rust-lang.org/t/anonymous-trait-use/1177](http://discuss.rust-lang.org/t/anonymous-trait-use/1177)

To address the visibility issue, perhaps the “use trait” syntax would be more useful, if it’s made mandatory/lint-enforced for traits.

---

<div class="post-metadata">

**Author:** ![Screwtapello](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/screwtapello/32/1076_2.png) [@Screwtapello](https://internals.rust-lang.org/u/Screwtapello)\
**Post date:** [January 7, 2015, 9:30pm UTC](https://internals.rust-lang.org/t/trait-importing-syntax/1209/5 "2015-01-07T21:30:09Z")

</div>

> [@d3zd3z](#):
>
> Does this still seem worthwhile to distinguish via syntax?

If everybody always sticks to the naming conventions, then that makes things better, but not perfect. I still think it's important for things with different meanings (import name vs. activate trait) to have different appearances, though.

---

<div class="post-metadata">

**Author:** ![gsingh93](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gsingh93/32/6884_2.png) [@gsingh93](https://internals.rust-lang.org/u/gsingh93)\
**Post date:** [January 20, 2015, 6:09am UTC](https://internals.rust-lang.org/t/trait-importing-syntax/1209/6 "2015-01-20T06:09:35Z")

</div>

Why do traits even need to be imported explicitly? If I import some struct `Foo`, I can use any of the methods defined in the `impl` for `Foo`. Why can’t I also call any method in a trait `impl` for `Foo`, and have that resolved without importing the trait itself?

Constantly having to add imports for these methods and having to track down which module the trait is located in is really annoying and slows me down while developing.

---

<div class="post-metadata">

**Author:** ![sanxiyn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sanxiyn/32/8824_2.png) [@sanxiyn](https://internals.rust-lang.org/u/sanxiyn)\
**Post date:** [January 20, 2015, 7:26am UTC](https://internals.rust-lang.org/t/trait-importing-syntax/1209/7 "2015-01-20T07:26:17Z")

</div>

Because you don’t know which trait to use. Variable foo is of type struct Foo. struct Foo implements trait T1, which defines method m. struct Foo also implements trait T2, which also defines method m. You call foo.m. Which one does it resolve to?

---

<div class="post-metadata">

**Author:** ![gsingh93](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gsingh93/32/6884_2.png) [@gsingh93](https://internals.rust-lang.org/u/gsingh93)\
**Post date:** [January 20, 2015, 1:34pm UTC](https://internals.rust-lang.org/t/trait-importing-syntax/1209/8 "2015-01-20T13:34:30Z")

</div>

For some reason I had always thought the methods on an object defined by traits had to be unique. If you call `Foo::bar()` in one file, and then call `Foo::bar()` in another file and have them refer to two completely different functions based on what trait was imported, that would be very confusing.

But after thinking about it more, this makes sense. A struct should be able to implement any trait, and shouldn’t be restricted by any other traits already implemented. You may want to implement two useful traits on an object, and you should be able to even if the trait authors didn’t know anything about the other trait and used the same method names unknowingly.

But in the majority of cases, there’s only one unique method you can choose. Can we not require the import in these cases, and only require the import if it’s ambiguous?

---

<div class="post-metadata">

**Author:** ![sanxiyn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sanxiyn/32/8824_2.png) [@sanxiyn](https://internals.rust-lang.org/u/sanxiyn)\
**Post date:** [January 20, 2015, 2:35pm UTC](https://internals.rust-lang.org/t/trait-importing-syntax/1209/9 "2015-01-20T14:35:45Z")

</div>

> Can we not require the import in these cases, and only require the import if it's ambiguous?

We can do that, but that means simply adding a new dependency to your project without modifying any source code can break the project by making methods ambiguous. When that happens, you need to add imports to all places you currently need to import.

I am sorry to say this, but all of these are common knowledge and have been explained over and over in the past. Don't be surprised if people simply ignore you if you ask "Why do traits even need to be imported explicitly?".

---

<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:** [March 25, 2019, 8:23am UTC](https://internals.rust-lang.org/t/trait-importing-syntax/1209/10 "2019-03-25T08:23:33Z")

</div>

Closed on March 25, 2019, 8:23am UTC

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