当前位置: 首页 > news >正文

rust持续学习 get_or_insert_with

通常使用一个值
if(x===null)x=some_valid_value
忽然今天看见一段代码

pub fn get_id() -> u64 
{
let mut res = struct.data.borrow_mut();
*res.get_or_insert_with(||{let mut xx = ...... some logiclet id = xx.id; id});
}

感觉这个名字蛮奇怪的 insert
然后翻了一下代码,是来自option.rs,原来就是空则赋值,这个写法对我来说蛮新奇的

    #[inline]#[stable(feature = "option_entry", since = "1.20.0")]#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]pub const fn get_or_insert_with<F>(&mut self, f: F) -> &mut TwhereF: ~const FnOnce() -> T,F: ~const Destruct,{if let None = *self {// the compiler isn't smart enough to know that we are not dropping a `T`// here and wants us to ensure `T` can be dropped at compile time.mem::forget(mem::replace(self, Some(f())))}// SAFETY: a `None` variant for `self` would have been replaced by a `Some`// variant in the code above.unsafe { self.as_mut().unwrap_unchecked() }}
http://www.lryc.cn/news/111568.html

相关文章:

  • 卡尔曼滤波 | Matlab实现无迹kalman滤波仿真
  • C++---list常用接口和模拟实现
  • [openCV]基于赛道追踪的智能车巡线方案V1
  • SpringIoc-个人学习笔记
  • 【一文搞懂泛型】
  • 概念解析 | 利用MIMO雷达技术实现高性能目标检测的关键技术解析
  • Grafana制作图表-自定义Flink监控图表
  • 【TypeScript】初识TypeScript和变量类型介绍
  • 阿里云瑶池 PolarDB 开源官网焕新升级上线
  • 泡水书为什么不能再出售
  • Mac 执行 .sh命令报错 command not found
  • postgresql 使用之 存储架构 触摸真实数据的存储结构以及组织形式,存入数据库的数据原来在这里
  • Node.Js安装与配置教程
  • Element-Plus DatePicker获取时间戳
  • 【算法第十五天7.29】513.找树左下角的值 112. 路径总和 106.从中序与后序遍历序列构造二叉树
  • Java thymeleaf bug排查记录
  • 互感和励磁电感(激磁电感)的关系
  • stdexcept和exception,两个头文件的区别?
  • openCV图像的读写操作
  • Android平台GB28181设备接入端如何降低资源占用和性能消耗
  • Android Studio安装AI编程助手Github Copilot
  • windows部署springboot项目 jar项目 (带日志监听和开机自起脚本)
  • 【数据结构和算法】排序算法
  • Error: Cannot find module ‘@babel/core’处理
  • K8S系列文章之 自动化运维利器 Fabric
  • flask--->CBV/模板/请求响应/session
  • Go语言基础:运算符、文件操作、接口、Packages、if else、for循环
  • 2308C++学习简单协程文档
  • C++笔记之从数组指针到函数数组指针(使用using name和std::function)
  • 【数据结构】常见的排序算法