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>


分享到:


相關文章: