0
点赞
收藏
分享

微信扫一扫

Rust 编程视频教程(进阶)——011_3 解引用多态


视频地址

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

源码地址

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

讲解内容

1、解引用强制多态
例子:接上面例子

fn hello(name: &str) { 
println!("Hello, {}!", name);
}

fn main() {
let m = MyBox::new(String::from("Rust"));
hello(&m); //此处解引用时,强制多态,将&String变为&str,否则的话此处需要:hello(&(*m)[..]);
}

2、解引用多态与可变性交互
解引用多态有如下三种情况:

  • 当 T: Deref<Target=U> 时从 &T 到 &U。
  • 当 T: DerefMut<Target=U> 时从 &mut T 到 &mut U。
  • 当 T: Deref<Target=U> 时从 &mut T 到 &U。(注意:此处反之是不可能的)


举报

相关推荐

0 条评论