Skip to content

Commit c722d1b

Browse files
authored
Fix static lifetime example which uses rand
1 parent 2c9b490 commit c722d1b

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/scope/lifetime/static_lifetime.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,30 @@ demonstrate, the below example uses
6969
to dynamically create `'static` references. In that case it definitely doesn't
7070
live for the entire duration, but only from the leaking point onward.
7171

72+
Note that you can execute the below code on your computer locally using the
73+
installable [`cargo play`](https://crates.io/crates/cargo-play) command.
74+
7275
```rust,editable,compile_fail
73-
extern crate rand;
74-
use rand::Fill;
76+
//# rand = "*"
77+
78+
use rand::Fill as _; // _ to prevent warnings
7579
76-
fn random_vec() -> &'static [usize; 100] {
77-
let mut rng = rand::thread_rng();
78-
let mut boxed = Box::new([0; 100]);
79-
boxed.try_fill(&mut rng).unwrap();
80+
fn random_vec() -> &'static [u32; 5] {
81+
let mut rng = rand::rng();
82+
let mut boxed = Box::new([0; 5]);
83+
boxed.fill(&mut rng);
8084
Box::leak(boxed)
8185
}
8286
8387
fn main() {
84-
let first: &'static [usize; 100] = random_vec();
85-
let second: &'static [usize; 100] = random_vec();
86-
assert_ne!(first, second)
88+
let first: &'static [u32; 5] = random_vec();
89+
let second: &'static [u32; 5] = random_vec();
90+
91+
// Surely they don't all happen to be the same...
92+
assert_ne!(first, second);
93+
94+
println!("first: {first:?}");
95+
println!("second: {second:?}");
8796
}
8897
```
8998

0 commit comments

Comments
 (0)