Skip to content

Commit 9737ae5

Browse files
committed
Added information about import renaming to the guide level explanation.
1 parent 76f9991 commit 9737ae5

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

text/0000-import-trait-methods.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,24 @@ mod b {
8080
}
8181
```
8282

83-
You cannot import a parent trait method from a sub-trait.
83+
Trait methods can also be renamed when they are imported using the usual `as` syntax:
84+
```rust
85+
use Default::default as gimme
86+
87+
struct S {
88+
a: HashMap<i32, i32>,
89+
}
90+
91+
impl S {
92+
fn new() -> S {
93+
S {
94+
a: gimme()
95+
}
96+
}
97+
}
98+
```
99+
100+
You cannot import a parent trait method from a sub-trait:
84101

85102
```rust
86103
use num_traits::float::Float::zero; // Error: try `use num_traits::identities::Zero::zero` instead.
@@ -142,11 +159,11 @@ Trait::method(x, y, z);
142159

143160
Additionally, the syntax
144161
```rust
145-
use Trait::self;
162+
use some_module::Trait::self;
146163
```
147164
is sugar for
148165
```rust
149-
use Trait;
166+
use some_module::Trait;
150167
```
151168

152169
The restriction on importing parent trait methods is a consequence of this desugaring, see https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=51bef9ba69ce1fc20248e987bf106bd4 for examples of the errors you get when you try to call parent trait methods through a child trait. We will likely want better error messages than this if a user tries to import a parent method.

0 commit comments

Comments
 (0)