美文网首页RUST编程
Rust 编程视频教程(进阶)——025_5 实现不安全的 tr

Rust 编程视频教程(进阶)——025_5 实现不安全的 tr

作者: 令狐壹冲 | 来源:发表于2020-03-06 20:05 被阅读0次

视频地址

头条地址:https://www.ixigua.com/i6775861706447913485
B站地址:https://www.bilibili.com/video/av81202308/

源码地址

github地址:https://github.com/anonymousGiga/learn_rust

讲解内容

(1)当至少有一个方法中包含编译器不能验证的不变量时,该 trait 是不安全的;
(2)在 trait 之前增加 unsafe 关键字将 trait 声明为 unsafe,同时 trait 的实现也必须标记为 unsafe。
例子:

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!");
}

相关文章

网友评论

    本文标题:Rust 编程视频教程(进阶)——025_5 实现不安全的 tr

    本文链接:https://www.haomeiwen.com/subject/zldnrhtx.html