Edited the static.md
This commit is contained in:
parent
e8fe2ebba2
commit
5e85e6630f
|
@ -1,5 +1,5 @@
|
||||||
# &'static and T: 'static
|
# &'static and T: 'static
|
||||||
`'static` is a reserved lifetime name, you might have encountered it serveral times:
|
`'static` is a reserved lifetime name, you might have encountered it several times:
|
||||||
```rust
|
```rust
|
||||||
// A reference with 'static lifetime:
|
// A reference with 'static lifetime:
|
||||||
let s: &'static str = "hello world";
|
let s: &'static str = "hello world";
|
||||||
|
@ -15,7 +15,7 @@ As a reference lifetime, `&'static` indicates the data pointed to by the referen
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
1γππ There are several ways to make a variable with `'static` lifetime, two of them are stored in the read-only memory of the binaryγ
|
1γππ There are several ways to make a variable with `'static` lifetime, two of them are stored in the read-only memory of the binary.
|
||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ fn need_static(r : &'static str) {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
2γ ππππ Another way to make `'static` lifetime is using `Box::leak`
|
2, ππππ Another way to make `'static` lifetime is using `Box::leak`
|
||||||
```rust,editable
|
```rust,editable
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Config {
|
struct Config {
|
||||||
|
@ -59,7 +59,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
3γ π `&'static` only indicates that the data can live forever, not the reference. The latter one will be constrained by its scope.
|
3, π `&'static` only indicates that the data can live forever, not the reference. The latter one will be constrained by its scope.
|
||||||
```rust,editable
|
```rust,editable
|
||||||
fn main() {
|
fn main() {
|
||||||
{
|
{
|
||||||
|
@ -75,7 +75,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
4γ `&'static` can be coerced to a shorter lifetime.
|
4, `&'static` can be coerced to a shorter lifetime.
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
```rust,editable
|
```rust,editable
|
||||||
|
@ -108,9 +108,9 @@ fn main() {
|
||||||
## T: 'static
|
## T: 'static
|
||||||
As a trait bound, it means the type does not contain any non-static references. Eg. the receiver can hold on to the type for as long as they want and it will never become invalid until they drop it.
|
As a trait bound, it means the type does not contain any non-static references. Eg. the receiver can hold on to the type for as long as they want and it will never become invalid until they drop it.
|
||||||
|
|
||||||
It's important to understand this means that any owned data always passes a `'static `lifetime bound, but a reference to that owned data generally does noγ
|
It's important to understand this means that any owned data always passes a `'static `lifetime bound, but a reference to that owned data generally does no.
|
||||||
|
|
||||||
5γππ
|
5,ππ
|
||||||
```rust,editable
|
```rust,editable
|
||||||
/* Make it work */
|
/* Make it work */
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
@ -145,7 +145,7 @@ fn main() {
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
6γπππ
|
6,πππ
|
||||||
```rust,editable
|
```rust,editable
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
|
Loadingβ¦
Reference in New Issue