gpt4 book ai didi

rust - 有没有办法在 Rust 中将类型标记为不可丢弃?

转载 作者:行者123 更新时间:2023-12-02 07:57:33 30 4
gpt4 key购买 nike

我想让它成为允许删除类型的编译器错误,而不是必须忘记它。我的用例是用于表示必须返回到其源进行清理的各种句柄的类型。这样 API 的用户就不会意外泄漏句柄。他们将被要求要么将句柄返回到其源,要么明确地忘记它。在源中,关联的资源将被清理并明确忘记句柄。

最佳答案

文章The Pain Of Real Linear Types in Rust提到这个。相关报价:

One extreme option that I've seen is to implement drop() as abort("this value must be used"). All "proper" consumers then mem::forget the value, preventing this "destructor bomb" from going off. This provides a dynamic version of strict must-use values. Although it's still vulnerable to the few ways destructors can leak, this isn't a significant concern in practice. Mostly it just stinks because it's dynamic and Rust users Want Static Verification.

Ultimately, Rust lacks "proper" support for this kind of type.



因此,假设您想要静态检查,答案是否定的。

关于rust - 有没有办法在 Rust 中将类型标记为不可丢弃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61699280/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com