Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加对drop(_x)函数的解释 #1254

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

rikkaka
Copy link

@rikkaka rikkaka commented Jul 24, 2023

这里对显式调用的drop(_x)函数未做解释,让人稍有困惑。我增加了一段注解,以让读者更好地理解drop(_x)的机理。

@@ -158,6 +158,8 @@ Bingo,完美拿走了所有权,而且这种实现保证了后续的使用必

细心的同学可能已经注意到,这里直接调用了 `drop` 函数,并没有引入任何模块信息,原因是该函数在[`std::prelude`](https://course.rs/appendix/prelude.html)里。

> 事实上,能被显式调用的drop(_x)函数只是个空函数,在拿走目标值的所有权后没有任何操作。而由于其持有目标值的所有权,在drop(_x)函数结束之际,编译器会执行_x真正的析构函数,从而完成释放资源的操作。换句话说,drop(_x)函数只是帮助目标值的所有者提前离开了作用域。
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一段有来源嘛?例如空函数的具体说明

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这是官方文档对此的说明
https://doc.rust-lang.org/std/mem/fn.drop.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants