-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Pedantic 'static
lifetime corrections
#1732
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,10 @@ confusion when learning Rust. Here are some examples for each situation: | |
## Reference lifetime | ||
|
||
As a reference lifetime `'static` indicates that the data pointed to by | ||
the reference lives for the entire lifetime of the running program. | ||
the reference lives for the remaining lifetime of the running program. | ||
It can still be coerced to a shorter lifetime. | ||
|
||
There are two ways to make a variable with `'static` lifetime, and both | ||
There are two common ways to make a variable with `'static` lifetime, and both | ||
are stored in the read-only memory of the binary: | ||
|
||
* Make a constant with the `static` declaration. | ||
|
@@ -62,6 +62,31 @@ fn main() { | |
} | ||
``` | ||
|
||
Since `'static` references only need to be valid for the _remainder_ of | ||
a program's life, they can created while the program is executed. Just to | ||
demonstrate, the below example uses | ||
[`Box::leak`](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak) | ||
to dynamically create `'static` references. In that case it definitely doesn't | ||
live for the entire duration, but only for the leaking point onward. | ||
|
||
```rust,editable,compile_fail | ||
extern crate rand; | ||
use rand::Fill; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to note that the rust playground allows use of the top 100 most downloaded crates, allowing |
||
|
||
fn random_vec() -> &'static [usize; 100] { | ||
let mut rng = rand::thread_rng(); | ||
let mut boxed = Box::new([0; 100]); | ||
boxed.try_fill(&mut rng).unwrap(); | ||
Box::leak(boxed) | ||
} | ||
|
||
fn main() { | ||
let first: &'static [usize; 100] = random_vec(); | ||
let second: &'static [usize; 100] = random_vec(); | ||
assert_ne!(first, second) | ||
} | ||
``` | ||
|
||
## Trait bound | ||
|
||
As a trait bound, it means the type does not contain any non-static | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
they can created
they can be created