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 协议》,转载必须注明作者和本文链接
推荐文章: