docs

#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 上編輯此頁 最後更新: