Rust 编程视频教程(进阶)——011_1 解引用介绍

视频地址#

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

源码地址#

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

讲解内容#

1、实现 Deref trait 允许我们重载解引用运算符。意思就是,如果 A 实现了 Deref trait,那么就可以写如下代码:

let a: A = A::new();
let b = &a;
let c = *b;//对A类型解引用

2、通过解引用使用指针的值。
例子:

fn main() {
    let x = 5;
    let y = &x;
    assert_eq!(5, x);
    assert_eq!(5, *y); //解引用
}

3、像引用一样使用 Box
例子:

fn main() {
    let x = 5;
    let y = Box::new(x);
    assert_eq!(5, x);
    assert_eq!(5, *y);
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
令狐一冲
文章
255
粉丝
121
喜欢
308
收藏
128
排名:328
访问:2.9 万
私信
所有博文
社区赞助商