-
Notifications
You must be signed in to change notification settings - Fork 13.3k
TRPL: const and static #24671
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: const and static #24671
Conversation
[let]: variable-bindings.html | ||
|
||
Constants live for the entire lifetime of a program, and therefore any | ||
reference stored in a constant has a [`’static` lifetime][lifetimes]: |
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 should be a regular apostrophe, not a curly one.
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.
i thought screwed one up, and I even double checked, sigh
bbcb26f
to
5ad21ca
Compare
[lifetimes]: lifetimes.html | ||
|
||
More specifically, constants in Rust have no fixed address in memory. This is | ||
because they’re effectively inlined to each place that they’re used. References |
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 sentence kinda contradicts the above claim that they can store any reference with a 'static
lifetime I think. I think that this may want to remove the reference to the 'static
lifetime and focus on the constant/inlining aspect.
Constants should be used for values that do not need an address in memory (as you've said), but they are allowed to reference addresses (e.g. the &'static str
case), but that's mostly an implementation detail that the contents of the reference are stored in static memory somewhere.
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.
Fair enough!
Some other topics this may wish to cover (but maybe not here?)
|
@alexcrichton so, when i was writing this, i almost thought they should be together. your additional topics pushed me over the edge. So now, they're combined together, and I tried to add some of the stuff you suggsted. Do you have any better justification for why const over static? |
It's pretty rare that you actually want a memory location associated with your constant, and using a So I guess the best analogy would be "should I use a Does that help? |
66921dc
to
1c6f3a4
Compare
@alexcrichton updated! |
constant are not necessarily guaranteed to refer to the same memory address for | ||
this reason. | ||
|
||
% `static` |
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.
Does this %
render ok? I think we specially handle the first line with a %
but not other lines?
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.
I suspect it's a typo for #?
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.
it is
r=me with a minor rendering nit Thanks @steveklabnik! |
1c6f3a4
to
a04f6c5
Compare
@bors: r=alexcrichton rollup |
📌 Commit a04f6c5 has been approved by |
⌛ Testing commit a04f6c5 with merge 50cd2e8... |
|
||
[let]: variable-bindings.html | ||
|
||
Constants live for the entire lifetime of a program. More specifically, |
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.
I think this would read easier as just:
Constants in Rust have no fixed address in memory because they're effectively inlined to each place they are used. For this reason, references to the same constant are not guaranteed to refer to the same memory address.
Particularly just eliminating the bit about how long constants live, because it's confusing compared to the description of statics.
r? @alexcrichton