Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/scope/lifetime/static_lifetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,30 @@ demonstrate, the below example uses
to dynamically create `'static` references. In that case it definitely doesn't
live for the entire duration, but only from the leaking point onward.

Note that you can execute the below code on your computer locally using the
installable [`cargo play`](https://crates.io/crates/cargo-play) command.

```rust,editable,compile_fail
extern crate rand;
use rand::Fill;
//# rand = "*"

use rand::Fill as _; // _ to prevent warnings

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();
fn random_vec() -> &'static [u32; 5] {
let mut rng = rand::rng();
let mut boxed = Box::new([0; 5]);
boxed.fill(&mut rng);
Box::leak(boxed)
}

fn main() {
let first: &'static [usize; 100] = random_vec();
let second: &'static [usize; 100] = random_vec();
assert_ne!(first, second)
let first: &'static [u32; 5] = random_vec();
let second: &'static [u32; 5] = random_vec();

// Surely they don't all happen to be the same...
assert_ne!(first, second);

println!("first: {first:?}");
println!("second: {second:?}");
}
```

Expand Down