Skip to content

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

Merged
merged 1 commit into from
Apr 22, 2015
Merged

TRPL: const and static #24671

merged 1 commit into from
Apr 22, 2015

Conversation

steveklabnik
Copy link
Member

[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]:
Copy link
Contributor

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.

Copy link
Member Author

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

@steveklabnik steveklabnik force-pushed the doc_const_static branch 2 times, most recently from bbcb26f to 5ad21ca Compare April 21, 2015 20:14
[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
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

@alexcrichton
Copy link
Member

Some other topics this may wish to cover (but maybe not here?)

  • Guidelines on when to choose a static or a const (it's almost always const)
  • Clarification that the type of a static must be Sync.
  • Clarification that initialization of both types must be a constant expression (e.g. no CTFE or anything)

@steveklabnik
Copy link
Member Author

@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?

@alexcrichton
Copy link
Member

It's pretty rare that you actually want a memory location associated with your constant, and using a const allows for optimizations like constant propagation not only in your crate but downstream crates. A const can be thought of as a #define in C where it has metadata overhead (e.g. it's in a header file or in Rust's metadata) but it has no runtime overhead (as the definition is inlined everywhere).

So I guess the best analogy would be "should I use a #define or a static in C" as it's largely the same question as whether you should use a const or a static in Rust.

Does that help?

@steveklabnik
Copy link
Member Author

@alexcrichton updated!

constant are not necessarily guaranteed to refer to the same memory address for
this reason.

% `static`
Copy link
Member

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?

Copy link
Member

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 #?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is

@alexcrichton
Copy link
Member

r=me with a minor rendering nit

Thanks @steveklabnik!

@steveklabnik
Copy link
Member Author

@bors: r=alexcrichton rollup

@bors
Copy link
Collaborator

bors commented Apr 21, 2015

📌 Commit a04f6c5 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Apr 22, 2015

⌛ Testing commit a04f6c5 with merge 50cd2e8...


[let]: variable-bindings.html

Constants live for the entire lifetime of a program. More specifically,
Copy link
Contributor

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.

@bors bors merged commit a04f6c5 into rust-lang:master Apr 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants