#E0092: 直接调用 Drop::drop
drop 在 using 块结束时会自动运行,再调用一次会把资源释放两次。要提前释放,提供一个 close() 这样的方法。
nabi
struct Conn { id: i32; }
impl Drop for Conn { fn drop(ref self) {} }
let c = Conn { id: 1 };
c.drop();
error[E0092]: `Drop::drop` cannot be called directly; it runs when a `using` block exits - expose a method such as `close()` to release early --> main.nabi:5:1 | 5 | c.drop(); | ^^^^^^^^
让 using 释放这个值:
nabi
struct Conn { id: i32; }
impl Drop for Conn { fn drop(ref self) { println(f"closed {self.id}"); } }
using c = (Conn { id: 1 }) {
println(f"using {c.id}");
}
using 1
closed 1
相关错误码:E0066 对没有 Drop 的值使用 using,E0133 资源在移动或关闭之后又被使用
发现错误? 在 GitHub 上编辑此页 最后更新: