mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-26 14:09:41 +00:00
fix: change 、to .
This commit is contained in:
@ -13,7 +13,7 @@ fn generic<T>(x: T) where T: 'static {}
|
||||
## &'static
|
||||
作为一个引用生命周期,`&'static` 说明该引用指向的数据可以跟程序活得一样久,但是该引用的生命周期依然有可能被强转为一个更短的生命周期。
|
||||
|
||||
1、🌟🌟 有好几种方法可以将一个变量标记为 `'static` 生命周期, 其中两种都是和保存在二进制文件中相关( 例如字符串字面量就是保存在二进制文件中,它的生命周期是 `'static` )。
|
||||
1. 🌟🌟 有好几种方法可以将一个变量标记为 `'static` 生命周期, 其中两种都是和保存在二进制文件中相关( 例如字符串字面量就是保存在二进制文件中,它的生命周期是 `'static` )。
|
||||
|
||||
```rust,editable
|
||||
|
||||
@ -30,7 +30,7 @@ fn need_static(r : &'static str) {
|
||||
}
|
||||
```
|
||||
|
||||
2、 🌟🌟🌟🌟 使用 `Box::leak` 也可以产生 `'static` 生命周期
|
||||
2. 🌟🌟🌟🌟 使用 `Box::leak` 也可以产生 `'static` 生命周期
|
||||
```rust,editable
|
||||
#[derive(Debug)]
|
||||
struct Config {
|
||||
@ -57,7 +57,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
3、 🌟 `&'static` 只能说明引用指向的数据是能一直存活的,但是引用本身依然受限于它的作用域
|
||||
3. 🌟 `&'static` 只能说明引用指向的数据是能一直存活的,但是引用本身依然受限于它的作用域
|
||||
```rust,editable
|
||||
fn main() {
|
||||
{
|
||||
@ -72,7 +72,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
4、 `&'static` 可以被强转成一个较短的生命周期
|
||||
4. `&'static` 可以被强转成一个较短的生命周期
|
||||
|
||||
**Example**
|
||||
```rust,editable
|
||||
@ -103,7 +103,7 @@ fn main() {
|
||||
|
||||
关于 `'static` 的特征约束详细解释,请参见 [Rust 语言圣经](https://course.rs/advance/lifetime/static.html#t-static),这里就不再赘述。
|
||||
|
||||
5、🌟🌟
|
||||
5. 🌟🌟
|
||||
```rust,editable
|
||||
/* 让代码工作 */
|
||||
use std::fmt::Debug;
|
||||
@ -137,7 +137,7 @@ fn main() {
|
||||
```
|
||||
|
||||
|
||||
6、🌟🌟🌟
|
||||
6. 🌟🌟🌟
|
||||
```rust,editable
|
||||
use std::fmt::Display;
|
||||
|
||||
|
Reference in New Issue
Block a user