summaryrefslogtreecommitdiffstats
path: root/src/doc/rust-by-example
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:36 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:36 +0000
commite02c5b5930c2c9ba3e5423fe12e2ef0155017297 (patch)
treefd60ebbbb5299e16e5fca8c773ddb74f764760db /src/doc/rust-by-example
parentAdding debian version 1.73.0+dfsg1-1. (diff)
downloadrustc-e02c5b5930c2c9ba3e5423fe12e2ef0155017297.tar.xz
rustc-e02c5b5930c2c9ba3e5423fe12e2ef0155017297.zip
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/doc/rust-by-example')
-rw-r--r--src/doc/rust-by-example/src/error/option_unwrap/defaults.md2
-rw-r--r--src/doc/rust-by-example/src/scope/borrow.md4
-rw-r--r--src/doc/rust-by-example/src/scope/lifetime/fn.md5
-rw-r--r--src/doc/rust-by-example/src/scope/lifetime/static_lifetime.md29
4 files changed, 34 insertions, 6 deletions
diff --git a/src/doc/rust-by-example/src/error/option_unwrap/defaults.md b/src/doc/rust-by-example/src/error/option_unwrap/defaults.md
index 4c1844a20..513bb1df7 100644
--- a/src/doc/rust-by-example/src/error/option_unwrap/defaults.md
+++ b/src/doc/rust-by-example/src/error/option_unwrap/defaults.md
@@ -113,7 +113,7 @@ fn main() {
### See also:
-[`closures`][closures], [`get_or_insert`][get_or_insert], [`get_or_insert_with`][get_or_insert_with], ,[`moved variables`][moved], [`or`][or], [`or_else`][or_else]
+[`closures`][closures], [`get_or_insert`][get_or_insert], [`get_or_insert_with`][get_or_insert_with], [`moved variables`][moved], [`or`][or], [`or_else`][or_else]
[closures]: https://doc.rust-lang.org/book/ch13-01-closures.html
[get_or_insert]: https://doc.rust-lang.org/core/option/enum.Option.html#method.get_or_insert
diff --git a/src/doc/rust-by-example/src/scope/borrow.md b/src/doc/rust-by-example/src/scope/borrow.md
index 72c70e736..018f00c35 100644
--- a/src/doc/rust-by-example/src/scope/borrow.md
+++ b/src/doc/rust-by-example/src/scope/borrow.md
@@ -20,7 +20,9 @@ fn borrow_i32(borrowed_i32: &i32) {
}
fn main() {
- // Create a boxed i32, and a stacked i32
+ // Create a boxed i32 in the heap, and a i32 on the stack
+ // Remember: numbers can have arbitrary underscores added for readability
+ // 5_i32 is the same as 5i32
let boxed_i32 = Box::new(5_i32);
let stacked_i32 = 6_i32;
diff --git a/src/doc/rust-by-example/src/scope/lifetime/fn.md b/src/doc/rust-by-example/src/scope/lifetime/fn.md
index 6c5c8afe6..095f84b6e 100644
--- a/src/doc/rust-by-example/src/scope/lifetime/fn.md
+++ b/src/doc/rust-by-example/src/scope/lifetime/fn.md
@@ -57,7 +57,8 @@ fn main() {
### See also:
-[functions][fn]
+[Functions][fn]
+
+[fn]: ../../fn.md
[elision]: elision.md
-[fn]: fn.md
diff --git a/src/doc/rust-by-example/src/scope/lifetime/static_lifetime.md b/src/doc/rust-by-example/src/scope/lifetime/static_lifetime.md
index 3f31693fa..3c0b8b0b7 100644
--- a/src/doc/rust-by-example/src/scope/lifetime/static_lifetime.md
+++ b/src/doc/rust-by-example/src/scope/lifetime/static_lifetime.md
@@ -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 be 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;
+
+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