Skip to content

Commit 17a50ea

Browse files
committed
impl-only-use
This RFC gives Rust the syntax: ```rust use foo::FooTrait as _; ``` Here, `_` means that we want to import the `FooTrait` trait’s impls but not the trait symbol directly, because we might declare the same symbol in the moduling issuing the `use`.
1 parent bc430eb commit 17a50ea

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

text/0000-impl-only-use.md

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
- Feature Name: impl-only-use
2+
- Start Date: 2017-10-01
3+
- RFC PR:
4+
- Rust Issue:
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
The `use … (… as …)` syntax can now accept a wildcard `_` as alias to a trait to only import the
10+
implementations of such a trait.
11+
12+
# Motivation
13+
[motivation]: #motivation
14+
15+
Sometimes, we might need to `use` a trait to be able to use its methods on a type in our code.
16+
However, we might also not want to import the trait symbol (because we redefine it, for instance):
17+
18+
```rust
19+
// in zoo.rs
20+
pub trait Zoo {
21+
fn zoo(&self) -> u32;
22+
}
23+
24+
// several impls here
25+
//
26+
```
27+
28+
```rust
29+
// in main.rs
30+
struct Zoo {
31+
//
32+
}
33+
34+
fn main() {
35+
let x = "foo";
36+
let y = x.zoo(); // won’t compile because `zoo::Zoo` not in scope
37+
}
38+
```
39+
40+
To solve this, we need to import the trait:
41+
42+
```rust
43+
// in main.rs
44+
use zoo::Zoo;
45+
46+
struct Zoo { // wait, what happens here?
47+
//
48+
}
49+
50+
fn main() {
51+
let x = "foo";
52+
let y = x.zoo(); // won’t compile because `zoo::Zoo` not in scope
53+
}
54+
```
55+
56+
However, you can see that we’ll hit a problem here, because we define an ambiguous symbol. We have
57+
two solutions:
58+
59+
- Change the name of the `struct` to something else.
60+
- Qualified the `use`.
61+
62+
The problem is that if we qualify the `use`, what name do we give the trait? We’re not even
63+
referring to it directly.
64+
65+
```rust
66+
use zoo::Zoo as ZooTrait;
67+
```
68+
69+
This will work but seems a bit like a hack because rustc forces us to give a name to something we
70+
won’t use in our types.
71+
72+
This RFC suggests to solve this by adding the possibility to explictly state that we won’t directly
73+
refer to that trait, but we want the impls:
74+
75+
```rust
76+
use zoo::Zoo as _;
77+
```
78+
79+
# Guide-level explanation
80+
[guide-level-explanation]: #guide-level-explanation
81+
82+
Qualifying a `use` with `_` on a trait imports the trait’s `impl`s but not the symbol directly. It’s
83+
handy if you don’t use the trait’s symbol in your type and if you redefine the symbol to something
84+
else.
85+
86+
The `_` means that you “don’t care about the name rustc will use for that qualified `use`“.
87+
88+
# Reference-level explanation
89+
[reference-level-explanation]: #reference-level-explanation
90+
91+
To be defined.
92+
93+
# Drawbacks
94+
[drawbacks]: #drawbacks
95+
96+
This RFC tries to solve a very specific problem (when you *must* alias a trait use). It’s just a
97+
nit to make the syntax more *“rust-ish”* (it’s very easy to think such a thing would work given the
98+
way `_` works pretty much everywhere else.
99+
100+
# Rationale and alternatives
101+
[alternatives]: #alternatives
102+
103+
The simple alternative is to let the programmer give a name to the qualified import, which is not a
104+
big deal, but is a bit ugly.
105+
106+
# Unresolved questions
107+
[unresolved]: #unresolved-questions

0 commit comments

Comments
 (0)