mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2025-02-23 04:32:16 +08:00
commit
bf10fb113c
@ -1,40 +1,19 @@
|
|||||||
## `Drop` Trait 运行清理代码
|
## 使用 `Drop` Trait 运行清理代码
|
||||||
|
|
||||||
> [ch15-03-drop.md](https://github.com/rust-lang/book/blob/master/second-edition/src/ch15-03-drop.md)
|
> [ch15-03-drop.md](https://github.com/rust-lang/book/blob/master/second-edition/src/ch15-03-drop.md)
|
||||||
> <br>
|
> <br>
|
||||||
> commit 721553e3a7b5ee9430cb548c8699b67be197b3f6
|
> commit 6060440d67759b7c8627b4d97cb69576057f5fa6
|
||||||
|
|
||||||
对于智能指针模式来说另一个重要的 trait 是 `Drop`。`Drop` 允许我们在值要离开作用域时执行一些代码。可以为任何类型提供 `Drop` trait 的实现,同时所指定的代码被用于释放类似于文件或网络连接的资源。我们在智能指针上下文中讨论 `Drop` 是因为其功能几乎总是用于实现智能指针。例如,`Box<T>` 自定义了 `Drop` 用来释放 box 所指向的堆空间。
|
对于智能指针模式来说另一个重要的 trait 是 `Drop`。`Drop` 允许我们在值要离开作用域时执行一些代码。可以为任何类型提供 `Drop` trait 的实现,同时所指定的代码被用于释放类似于文件或网络连接的资源。我们在智能指针上下文中讨论 `Drop` 是因为其功能几乎总是用于实现智能指针。例如,`Box<T>` 自定义了 `Drop` 用来释放 box 所指向的堆空间。
|
||||||
|
|
||||||
在其他一些语言中,我们不得不记住在每次使用完智能指针实例后调用清理内存或资源的代码。如果忘记的话,运行代码的系统可能会因为负荷过重而崩溃。在 Rust 中,可以指定一些代码应该在值离开作用域时被执行,而编译器会自动插入这些代码。
|
在其他一些语言中,我们不得不记住在每次使用完智能指针实例后调用清理内存或资源的代码。如果忘记的话,运行代码的系统可能会因为负荷过重而崩溃。在 Rust 中,可以指定一些代码应该在值离开作用域时被执行,而编译器会自动插入这些代码。于是我们就不需要在程序中到处编写在实例结束时清理这些变量的代码——而且还不会泄露资源了。
|
||||||
|
|
||||||
<!-- Are we saying that any code can be run, and that we can use that to clean
|
|
||||||
up, or that this code that can be run is specifically always for clean up? -->
|
|
||||||
<!-- I don't understand what the difference between those two choices are?
|
|
||||||
/Carol -->
|
|
||||||
|
|
||||||
这意味着无需记住在所有处理完这些类型实例后调用清理代码,而仍然不会泄露资源!
|
这意味着无需记住在所有处理完这些类型实例后调用清理代码,而仍然不会泄露资源!
|
||||||
|
|
||||||
指定在值离开作用域时应该执行的代码的方式是实现 `Drop` trait。`Drop` trait 要求实现一个叫做 `drop` 的方法,它获取一个 `self` 的可变引用。为了能够看出 Rust 何时调用 `drop`,让我们暂时使用 `println!` 语句实现 `drop`。
|
指定在值离开作用域时应该执行的代码的方式是实现 `Drop` trait。`Drop` trait 要求实现一个叫做 `drop` 的方法,它获取一个 `self` 的可变引用。为了能够看出 Rust 何时调用 `drop`,让我们暂时使用 `println!` 语句实现 `drop`。
|
||||||
|
|
||||||
<!-- Why are we showing this as an example and not an example of it being used
|
|
||||||
for clean up? -->
|
|
||||||
<!-- To demonstrate the mechanics of implementing the trait and showing when
|
|
||||||
this code gets run. It's hard to experience the cleaning up unless we print
|
|
||||||
something. /Carol -->
|
|
||||||
|
|
||||||
示例 15-16 展示了唯一定制功能就是当其实例离开作用域时打印出 `Dropping CustomSmartPointer!` 的结构体 `CustomSmartPointer`。这会演示 Rust 何时运行 `drop` 函数:
|
示例 15-16 展示了唯一定制功能就是当其实例离开作用域时打印出 `Dropping CustomSmartPointer!` 的结构体 `CustomSmartPointer`。这会演示 Rust 何时运行 `drop` 函数:
|
||||||
|
|
||||||
<!-- Is this below just telling us how to adapt it for cleaning up instead?
|
|
||||||
Maybe save it for when we have context for it? Instead of a `println!`
|
|
||||||
statement, you'd fill in `drop` with whatever cleanup code your smart pointer
|
|
||||||
needs to run: -->
|
|
||||||
<!-- This is demonstrating what we need to do to use `Drop`, without getting
|
|
||||||
into the complexities of what "cleaning up" might mean yet, just to give the
|
|
||||||
reader an idea of when this code gets called and that it gets called
|
|
||||||
automatically. We're building up to cleaning up. /Carol -->
|
|
||||||
|
|
||||||
|
|
||||||
<span class="filename">文件名: src/main.rs</span>
|
<span class="filename">文件名: src/main.rs</span>
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@ -55,16 +34,11 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<span class="caption">示例 15-16:结构体 `CustomSmartPointer`,其实现了放置清理代码的 `Drop` trait</span>
|
<span class="caption">示例 15-14:结构体 `CustomSmartPointer`,其实现了放置清理代码的 `Drop` trait</span>
|
||||||
|
|
||||||
`Drop` trait 包含在 prelude 中,所以无需导入它。我们在 `CustomSmartPointer` 上实现了 `Drop` trait,并提供了一个调用 `println!` 的 `drop` 方法实现。`drop` 函数体是放置任何当类型实例离开作用域时期望运行的逻辑的地方。这里选择打印一些文本以展示 Rust 合适调用 `drop`。
|
`Drop` trait 包含在 prelude 中,所以无需导入它。我们在 `CustomSmartPointer` 上实现了 `Drop` trait,并提供了一个调用 `println!` 的 `drop` 方法实现。`drop` 函数体是放置任何当类型实例离开作用域时期望运行的逻辑的地方。这里选择打印一些文本以展示 Rust 合适调用 `drop`。
|
||||||
|
|
||||||
<!-- Where you'd put this code, or where this code would be called? It seems
|
在 `main` 中,我们新建了两个 `CustomSmartPointer` 实例并打印出了 `CustomSmartPointer created.`。在 `main` 的结尾,`CustomSmartPointer` 的实例会离开作用域,而 Rust 会调用放置于 `drop` 方法中的代码,打印出最后的信息。注意无需显示调用 `drop` 方法:
|
||||||
laborious to write this clean up code wherever there's a print call? -->
|
|
||||||
<!-- I'm not sure how you concluded that from what we had here, could you
|
|
||||||
elaborate? /Carol -->
|
|
||||||
|
|
||||||
在 `main` 中,新建了一个 `CustomSmartPointer` 实例并打印出了 `CustomSmartPointer created.`。在 `main` 的结尾,`CustomSmartPointer` 的实例会离开作用域,而 Rust 会调用放置于 `drop` 方法中的代码,打印出最后的信息。注意无需显示调用 `drop` 方法:
|
|
||||||
|
|
||||||
当运行这个程序,会出现如下输出:
|
当运行这个程序,会出现如下输出:
|
||||||
|
|
||||||
@ -74,27 +48,13 @@ Dropping CustomSmartPointer with data `other stuff`!
|
|||||||
Dropping CustomSmartPointer with data `my stuff`!
|
Dropping CustomSmartPointer with data `my stuff`!
|
||||||
```
|
```
|
||||||
|
|
||||||
当实例离开作用域 Rust 会自动调用 `drop`,并调用我们指定的代码。变量以被创创建时相反的顺序被丢弃,所以 `d` 在 `c` 之前被丢弃。这刚好给了我们一个 drop 方法如何工作的可视化指导,不过通常需要指定类型所需执行的清理代码而不是打印信息。
|
当实例离开作用域 Rust 会自动调用 `drop`,并调用我们指定的代码。变量以被创建时相反的顺序被丢弃,所以 `d` 在 `c` 之前被丢弃。这个例子刚好给了我们一个 drop 方法如何工作的可视化指导,不过通常需要指定类型所需执行的清理代码而不是打印信息。
|
||||||
|
|
||||||
<!-- Can you wrap this example up by saying what you would actually put in a
|
|
||||||
drop method and why?-->
|
|
||||||
<!-- Done /Carol -->
|
|
||||||
|
|
||||||
#### 通过 `std::mem::drop` 提早丢弃值
|
#### 通过 `std::mem::drop` 提早丢弃值
|
||||||
|
|
||||||
<!-- is this a new method from Drop or the same method? -->
|
不幸的是,我们并不能直截了当的禁用 `drop` 这个功能。通常也不需要禁用 `drop` ;整个 `Drop` trait 存在的意义在于其是自动处理的。然而,有时你可能需要提早清理某个值。一个例子是当使用智能指针管理锁时;你可能希望强制运行 `drop` 方法来释放锁以便作用域中的其他代码可以获取锁。Rust 并不允许我们主动调用 `Drop` trait 的 `drop` 方法;当我们希望在作用域结束之前就释放变量的话,我们应该使用的是由标准库提供的 `std::mem::drop`。
|
||||||
<!-- This is a new function. /Carol -->
|
|
||||||
|
|
||||||
Rust 当值离开作用域时自动插入 `drop` 调用,不能直接禁用这个功能。
|
如果我们像是示例 15-14 那样尝试调用 `Drop` trait 的 `drop` 方法,就会得到像示例 15-15 那样的编译错误:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
被打印到屏幕上,它展示了 Rust 在实例离开作用域时自动调用了`drop`。通常也不需要禁用 `drop`;整个 `Drop` trait 存在的意义在于其是自动处理的。有时可能需要提早清理某个值。一个例子是当使用智能指针管理锁时;你可能希望强制运行 `drop` 方法来释放锁以便作用域中的其他代码可以获取锁。首先。让我们看看自己调用 `Drop` trait 的 `drop` 方法会发生什么,如示例 15-17 修改示例 15-16 中的 `main` 函数:
|
|
||||||
|
|
||||||
<!-- Above: I'm not following why we are doing this, if it's not necessary and
|
|
||||||
we aren't going to cover it now anyway -- can you lay out why we're discussing
|
|
||||||
this here? -->
|
|
||||||
<!-- Done. /Carol -->
|
|
||||||
|
|
||||||
<span class="filename">文件名: src/main.rs</span>
|
<span class="filename">文件名: src/main.rs</span>
|
||||||
|
|
||||||
@ -107,15 +67,15 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<span class="caption">示例 15-17:尝试手动调用 `Drop` trait 的 `drop` 方法提早清理</span>
|
<span class="caption">示例 15-15:尝试手动调用 `Drop` trait 的 `drop` 方法提早清理</span>
|
||||||
|
|
||||||
如果尝试编译代码会得到如下错误:
|
如果尝试编译代码会得到如下错误:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
error[E0040]: explicit use of destructor method
|
error[E0040]: explicit use of destructor method
|
||||||
--> src/main.rs:15:7
|
--> src/main.rs:14:7
|
||||||
|
|
|
|
||||||
15 | c.drop();
|
14 | c.drop();
|
||||||
| ^^^^ explicit destructor calls not allowed
|
| ^^^^ explicit destructor calls not allowed
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -125,7 +85,7 @@ Rust 不允许我们显式调用 `drop` 因为 Rust 仍然会在 `main` 的结
|
|||||||
|
|
||||||
因为不能禁用当值离开作用域时自动插入的 `drop`,并且不能显示调用 `drop`,如果我们需要提早清理值,可以使用 `std::mem::drop` 函数。
|
因为不能禁用当值离开作用域时自动插入的 `drop`,并且不能显示调用 `drop`,如果我们需要提早清理值,可以使用 `std::mem::drop` 函数。
|
||||||
|
|
||||||
`std::mem::drop` 函数不同于 `Drop` trait 中的 `drop` 方法。可以通过传递希望提早强制丢弃的值作为参数。`std::mem::drop` 位于 prelude,所以我们可以修改示例 15-16 中的 `main` 来调用 `drop` 函数如示例 15-18 所示:
|
`std::mem::drop` 函数不同于 `Drop` trait 中的 `drop` 方法。可以通过传递希望提早强制丢弃的值作为参数。`std::mem::drop` 位于 prelude,所以我们可以修改示例 15-15 中的 `main` 来调用 `drop` 函数如示例 15-16 所示:
|
||||||
|
|
||||||
<span class="filename">文件名: src/main.rs</span>
|
<span class="filename">文件名: src/main.rs</span>
|
||||||
|
|
||||||
@ -148,7 +108,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<span class="caption">示例 15-18: 在值离开作用域之前调用 `std::mem::drop` 显式清理</span>
|
<span class="caption">示例 15-16: 在值离开作用域之前调用 `std::mem::drop` 显式清理</span>
|
||||||
|
|
||||||
运行这段代码会打印出如下:
|
运行这段代码会打印出如下:
|
||||||
|
|
||||||
@ -158,24 +118,12 @@ Dropping CustomSmartPointer with data `some data`!
|
|||||||
CustomSmartPointer dropped before the end of main.
|
CustomSmartPointer dropped before the end of main.
|
||||||
```
|
```
|
||||||
|
|
||||||
<!-- What's the destructor code, here? We haven't mentioned that before, not in
|
|
||||||
this chapter in any case -->
|
|
||||||
<!-- I added a definition for destructor a few paragraphs above, the first time
|
|
||||||
we see it in an error message. /Carol -->
|
|
||||||
|
|
||||||
```Dropping CustomSmartPointer with data `some data`!``` 出现在 `CustomSmartPointer created.` 和 `CustomSmartPointer dropped before the end of main.` 之间,表明了 `drop` 方法被调用了并在此丢弃了 `c`。
|
```Dropping CustomSmartPointer with data `some data`!``` 出现在 `CustomSmartPointer created.` 和 `CustomSmartPointer dropped before the end of main.` 之间,表明了 `drop` 方法被调用了并在此丢弃了 `c`。
|
||||||
|
|
||||||
<!-- How does this show that the destructor code (is that drop?) is called? Is
|
|
||||||
this correct, above?-->
|
|
||||||
<!-- The order of what gets printed shows that the drop code is called.
|
|
||||||
/Carol-->
|
|
||||||
|
|
||||||
`Drop` trait 实现中指定的代码可以用于许多方面来使得清理变得方便和安全:比如可以用其创建我们自己的内存分配器!通过 `Drop` trait 和 Rust 所有权系统,你无需担心之后清理代码,Rust 会自动考虑这些问题。
|
`Drop` trait 实现中指定的代码可以用于许多方面来使得清理变得方便和安全:比如可以用其创建我们自己的内存分配器!通过 `Drop` trait 和 Rust 所有权系统,你无需担心之后清理代码,Rust 会自动考虑这些问题。
|
||||||
|
|
||||||
我们也无需担心意外的清理掉仍在使用的值,这会造成编译器错误:所有权系统确保引用总是有效的,也会确保 `drop` 只会在值不再被使用时被调用一次。
|
我们也无需担心意外的清理掉仍在使用的值,这会造成编译器错误:所有权系统确保引用总是有效的,也会确保 `drop` 只会在值不再被使用时被调用一次。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
使用 `Drop` trait 实现指定的代码在很多方面都使得清理值变得方便和安全:比如可以使用它来创建我们自己的内存分配器!通过`Drop` trait 和 Rust 所有权系统,就无需担心之后清理代码,因为 Rust 会自动考虑这些问题。如果代码在值仍被使用时就清理它会出现编译错误,因为所有权系统确保了引用总是有效的,这也就保证了`drop`只会在值不再被使用时被调用一次。
|
使用 `Drop` trait 实现指定的代码在很多方面都使得清理值变得方便和安全:比如可以使用它来创建我们自己的内存分配器!通过`Drop` trait 和 Rust 所有权系统,就无需担心之后清理代码,因为 Rust 会自动考虑这些问题。如果代码在值仍被使用时就清理它会出现编译错误,因为所有权系统确保了引用总是有效的,这也就保证了`drop`只会在值不再被使用时被调用一次。
|
||||||
|
|
||||||
现在我们学习了 `Box<T>` 和一些智能指针的特性,让我们聊聊一些其他标准库中定义的智能指针。
|
现在我们学习了 `Box<T>` 和一些智能指针的特性,让我们聊聊一些其他标准库中定义的智能指针。
|
Loading…
Reference in New Issue
Block a user