03.06 Rust 編程視頻教程(進階)——025

視頻地址

頭條地址:https://www.ixigua.com/i6775861706447913485

源碼地址

github地址:見擴展鏈接。

講解內容

(1)當至少有一個方法中包含編譯器不能驗證的不變量時,該 trait 是不安全的;(2)在 trait 之前增加 unsafe 關鍵字將 trait 聲明為 unsafe,同時 trait 的實現也必須標記為 unsafe。例子:

<code>struct Bar();
unsafe trait Foo {
fn foo(&self);
}
unsafe impl Foo for Bar{
fn foo(&self) {
println!("foo");
}
}
fn main() {
let a: Bar = Bar();
a.foo();
println!("Hello, world!");
}/<code>


分享到:


相關文章: