-
Notifications
You must be signed in to change notification settings - Fork 13.6k
TRPL: associated constants #24990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TRPL: associated constants #24990
Conversation
r? @huonw (rust_highfive has picked a reviewer for you, use r? to override) |
@@ -0,0 +1,66 @@ | |||
% Associated Constants | |||
|
|||
With the `associated_constants` feature, you can define constants like this: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
associated_constants => associated_consts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoops!
I believe there's also an allowance for associated constants of the form: pub struct Foo;
impl Foo {
pub const FOO: u32 = 3;
} e.g. an inherent constant associated to a type (maybe worth mentioning?) |
cc @quantheory |
8b13f19
to
b1d37ae
Compare
Yeah, worth mentioning for sure. Added, and nits fixed. |
with `i32`. It will then use the default value. But, as in `i64`, we can also | ||
add our own definition. | ||
|
||
Associated constants don’t have to be with a trait. An `impl` block for a `struct` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This phrasing is slightly odd. Did you mean "associated with a trait"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I did.
LGTM. There is also the ability to define a constant of type |
b1d37ae
to
ba40051
Compare
Yeah, let's hold off on the |
|
||
fn main() { | ||
assert_eq!(1, <i32 as Foo>::ID); | ||
assert_eq!(5, <i64 as Foo>::ID); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As one final nit, I think these are more idiomatically referred to without the angle-bracket syntax, e.g. i32::ID
r=me with one final nit, thanks @steveklabnik! |
ba40051
to
6b465f5
Compare
@bors: r=alexcrichton rollup |
📌 Commit 6b465f5 has been approved by |
⌛ Testing commit 6b465f5 with merge 2e4fef0... |
💔 Test failed - auto-linux-32-nopt-t |
@bors: retry On Sat, May 2, 2015 at 10:44 AM, bors [email protected] wrote:
|
No description provided.