介绍

本系列录制的视频主要放在B站上Rust死灵书学习视频

Rust相关的源码资料在:https://github.com/anonymousGiga

笔记内容

一段代码

struct Inspector<'a>(&'a u8);struct World<'a> {inspector: Option<Inspector<'a>>,days: Box<u8>,
}fn main() {let mut world = World {inspector: None,days: Box::new(1),};world.inspector = Some(Inspector(&world.days));
}

加上Drop:

struct Inspector<'a>(&'a u8);impl<'a> Drop for Inspector<'a> {fn drop(&mut self) {println!("I was only {} days from retirement!", self.0);}
}struct World<'a> {inspector: Option<Inspector<'a>>,days: Box<u8>,
}fn main() {let mut world = World {inspector: None,days: Box::new(1),};world.inspector = Some(Inspector(&world.days));
}

Drop要求

一个安全地实现 Drop的类型,它的泛型参数生命周期必须严格地长于它本身。
ps:注意是泛型类型,如果不是泛型类型,则只有用static了

但是如下的代码是不行的:

//struct Inspector<'a>(&'a u8);struct Inspector<'a>(&'a u8, &'static str);//impl<'a> Drop for Inspector<'a> {unsafe impl<'#[may_dangle] a> Drop for Inspector<'a> {fn drop(&mut self) {//println!("I was only {} days from retirement!", self.0);println!("I was only {} days from retirement!", self.1);}
}struct World<'a> {inspector: Option<Inspector<'a>>,days: Box<u8>,
}fn main() {let mut world = World {inspector: None,days: Box::new(1),};//world.inspector = Some(Inspector(&world.days, "hello"));world.inspector = Some(Inspector(&world.days, "gadget"));
}

010 Rust死灵书之Drop检查相关推荐

  1. 017 Rust死灵书之Drop标志

    介绍 本系列录制的视频主要放在B站上Rust死灵书学习视频 Rust 死灵书相关的源码资料在https://github.com/anonymousGiga/Rustonomicon-Source d ...

  2. 029 Rust死灵书之Vec实现Drop

    介绍 本系列录制的视频主要放在B站上Rust死灵书学习视频 Rust 死灵书相关的源码资料在https://github.com/anonymousGiga/Rustonomicon-Source 详 ...

  3. 026 Rust死灵书之实现Vec

    介绍 本系列录制的视频主要放在B站上Rust死灵书学习视频 Rust 死灵书相关的源码资料在https://github.com/anonymousGiga/Rustonomicon-Source 从 ...

  4. 025 Rust死灵书之原子操作

    介绍 本系列录制的视频主要放在B站上Rust死灵书学习视频 Rust 死灵书相关的源码资料在https://github.com/anonymousGiga/Rustonomicon-Source 原 ...

  5. 019 Rust死灵书之资源管理

    本系列录制的视频主要放在B站上Rust死灵书学习视频 Rust 死灵书相关的源码资料在https://github.com/anonymousGiga/Rustonomicon-Source 基于所有 ...

  6. 035 Rust死灵书之Vec处理零尺寸类型

    介绍 本系列录制的视频主要放在B站上Rust死灵书学习视频 Rust 死灵书相关的源码资料在https://github.com/anonymousGiga/Rustonomicon-Source 详 ...

  7. 033 Rust死灵书之重构Vec

    介绍 本系列录制的视频主要放在B站上Rust死灵书学习视频 Rust 死灵书相关的源码资料在https://github.com/anonymousGiga/Rustonomicon-Source 详 ...

  8. 032 Rust死灵书之Vec的IntoIter

    介绍 本系列录制的视频主要放在B站上Rust死灵书学习视频 Rust 死灵书相关的源码资料在https://github.com/anonymousGiga/Rustonomicon-Source 详 ...

  9. 030 Rust死灵书之让Vec支持slice

    介绍 本系列录制的视频主要放在B站上Rust死灵书学习视频 Rust 死灵书相关的源码资料在https://github.com/anonymousGiga/Rustonomicon-Source 详 ...

最新文章

  1. Linux多线程的同步------读写锁
  2. chrome 禁用https限制http_HTTP协议走过29年漏洞百出:Firefox可直接禁用
  3. UA MATH566 统计理论 证明UMVUE的方法
  4. 258. Add Digits
  5. Qt5:渐变效果的实现
  6. 映客上云 助力《疫战到底》系列课程吸引数千万用户观看
  7. 【图像数据增强】Mixup、Cutout、Random Erasing、CutMix、Mosaic等详解
  8. 面试数据分析岗,怎么提升一倍成功率?让过来人给你支支招
  9. git-bisect last updated in 2.19.1【转】
  10. 网关 跨域_好文推荐:微服务网关实战—Spring Cloud Gateway
  11. matlab单机无限大系统_单机无穷大系统暂态仿真(完整).docx
  12. 协作中继认知无线电功率分配
  13. 联通4g满格但是网速慢_手机网速太慢怎么办 教你一招(4g信号满格网速很慢)...
  14. 如何选择DDoS防御服务器
  15. 【iOS】—— weak的基本原理
  16. 2023年有哪些值得推荐的蓝牙耳机?小米|南卡|JBL等等高性价比蓝牙耳机推荐
  17. 从零开始学视觉里程计——一个初学者教程
  18. vb.net与SQL Server
  19. 不同股票的账面市值比(2012-2020年)
  20. pl/sql编程----触发器

热门文章

  1. 36岁互联网高管从大厂裸辞,专门卖书快乐多了
  2. vue横向的table表格
  3. 【洛谷P3014】Cow Line
  4. 小白scrapy试炼-爬取慕课网免费课程
  5. plant simulation 建模案例分析
  6. python学习笔记 selenium
  7. 视频剪辑必备的6个免费素材库~
  8. java 不是封闭类_java – 不是一个封闭的类错误Android Studio
  9. signature=4209f9d53642a2c1cdf0e4a92ba622eb,HIERBA GUINEA (Panicum maximum Jacq.)
  10. Win10下安装必应输入法能导致Alt+Tab切换页面时不能置于最前