Skip to content

Commit 3f17ff8

Browse files
committed
First pass at adding associated constants to the RFC.
1 parent 6d1c8f2 commit 3f17ff8

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

text/0000-import-trait-associated-functions.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Summary
77
[summary]: #summary
88

9-
Allow importing associated functions from traits and then using them like regular functions.
9+
Allow importing associated functions and constants from traits and then using them like regular items.
1010

1111
# Motivation
1212
[motivation]: #motivation
@@ -15,6 +15,8 @@ There has for a long time been a desire to shorten the duplication needed to acc
1515

1616
Additionally, if you pull in a crate like [num_traits](https://docs.rs/num-traits/latest/num_traits/), then this feature will allow access to numeric functions such as `sin` using the `sin(x)` syntax that is more common in mathematics. More generally, it will make calls to trait associated functions shorter without having to write a wrapper function.
1717

18+
Similarly, associated constants, which act much like constant functions, can be imported to give easier access to them.
19+
1820
# Guide-level explanation
1921
[guide-level-explanation]: #guide-level-explanation
2022

@@ -108,15 +110,33 @@ fn main() {
108110
}
109111
```
110112

113+
Importing an associated constant is allowed too:
114+
```rust
115+
mod m {
116+
trait MyNumTrait {
117+
const ZERO: Self;
118+
const ONE: Self;
119+
}
120+
121+
// Impl for every numeric type...
122+
}
123+
124+
use m::MyNumTrait::ZERO;
125+
126+
fn f() -> u32 {
127+
ZERO
128+
}
129+
```
130+
111131
# Reference-level explanation
112132
[reference-level-explanation]: #reference-level-explanation
113133

114134
When
115135

116136
```rust
117-
use Trait::func as m;
137+
use Trait::item as m;
118138
```
119-
occurs, a new item `m` is made available in the value namespace of the current module. Any attempts to call this item are treated as calling the associated function explicitly qualified. As always, the `as` qualifier is optional, in which case the name of the new item is identical with the name of the associated function in the trait. In other words, the example:
139+
occurs, a new item `m` is made available in the value namespace of the current module. Any attempts to use this item are treated as using the associated item explicitly qualified. `item` must be either an associated function or an associated constant. As always, the `as` qualifier is optional, in which case the name of the new item is identical with the name of the associated item in the trait. In other words, the example:
120140

121141
```rust
122142
use Default::default;

0 commit comments

Comments
 (0)