Rust- match匹配

chatgpt/2023/9/26 12:55:36

The match expression in Rust is a powerful tool that allows you to handle multiple outcomes of a pattern match operation, very similar to a switch statement in other languages but way more powerful.

In its simplest form, it can be used to match values:

let value = 1;match value {1 => println!("one"),2 => println!("two"),_ => println!("something else"),
}

In the above example, the variable value is compared with the patterns in each arm of the match expression. The _ pattern is a catch-all that matches anything, and it’s usually used as the last arm.

However, Rust’s match is capable of a lot more than just comparing values. It can destructure complex data types:

enum OptionalInt {Value(i32),Missing,
}let x = OptionalInt::Value(5);match x {OptionalInt::Value(i) if i > 5 => println!("Got an int bigger than five!"),OptionalInt::Value(..) => println!("Got an int!"),OptionalInt::Missing => println!("No such int!"),
}

In this example, the match expression is used to handle the various possible states of an OptionalInt enum.

Rust also supports pattern guards, allowing for more complex logic:

let pair = (2, -2);
// TODO ^ Try different values for `pair`match pair {(x, y) if x == y => println!("These are twins"),(x, y) if x + y == 0 => println!("These numbers cancel each other out"),(x, _) if x % 2 == 1 => println!("The first one is odd"),_ => println!("No correlation..."),
}

In this example, match evaluates pairs of numbers and uses pattern guards to add conditions to the patterns.

In summary, match is a versatile tool in Rust that can be used to write clear and concise code. It enforces exhaustive checking, ensuring that all possibilities are handled.

A comprehensive case is as follows:

fn main() {/*解构 &、ref、ref mut解引用 **/let num = &100;match num {&val => println!("&val 是 {:?}", val), // &val 是 100}match *num {val => println!("val 是 {:?}", val), // val 是 100}// ref 改变了赋值的行为,可以对具体值创建引用let ref num3 = 66;// 定义2个非引用变量,通过ref和ref mut仍然可以得到其引用。let num4 = 5;let mut mut_num4 = 7;match num4 {ref r => println!("num4 是 {:?}", r), // num4 是 5}match mut_num4 {ref mut m => {*m += 10;println!("mut_num4 是 {:?}", m); // mut_num4 是 17}}let s = Study {name: String::from("Rust"),target: String::from("熟练书写Rust程序"),spend: 36,};let Study {name: name,target: target,spend: spend,} = s;println!("name = {}, target = {}, spend = {}", name, target, spend);// name = Rust, target = 熟练书写Rust程序, spend = 36let s2 = Study {name: String::from("Rust"),target: String::from("熟练书写Rust程序"),spend: 36,};let Study { name, .. } = s2;println!("name = {}", name); // name = Rust
}struct Study {name: String,target: String,spend: u32,
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.exyb.cn/news/show-5313863.html

如若内容造成侵权/违法违规/事实不符,请联系郑州代理记账网进行投诉反馈,一经查实,立即删除!

相关文章

VSCode搭建GCC环境

1. 下载 https://www.mingw-w64.org/downloads/ https://github.com/niXman/mingw-builds-binaries/releases 2.安装 x86_64-12.2.0-release-win32-seh-rt_v10-rev1.7z解压到D盘 我的电脑–属性–系统属性–环境变量–系统变量–path D:\MinGW-w64\x86_64-12.2.0-release…

【【萌新的stm32学习-2】】

萌新的stm32学习-2 STM32 启动模式 可以通过BOOT0和BOOT1 引脚设置启动模式 BOOT1 BOOT0 X 0 启动模式 主闪存存储器 0 1 系统存储器 1 1 选择 内置SRAM(少) 我们使用串口给 STM32 下载程序,但是串口下载并不能仿真调试代码,只…

react实现markdown

参考&#xff1a;https://blog.csdn.net/Jack_lzx/article/details/118495763 参考&#xff1a;https://blog.csdn.net/m0_48474585/article/details/119742984 0. 示例 用react实现markdown编辑器 1.基本布局及样式 <><div classNametf_editor_header>头部&…

前端面试的性能优化部分(3)每篇10题

21.如何优化移动端网页的性能&#xff1f; 优化移动端网页的性能是提升用户体验、降低用户流失的关键。以下是一些优化移动端网页性能的常见方法&#xff1a; 压缩和合并资源&#xff1a; 压缩 CSS、JavaScript 和图片等静态资源&#xff0c;减少文件大小&#xff0c;同时合并…

解决:h5的<video>在移动端浏览器无法自动播放

并不是所有的移动端浏览器都无法自动播放&#xff0c;下载谷歌、火狐、edge等都可以正常播放&#xff0c;目前发现夸克浏览器无法自动播放。即autoplay属性失效。 <video autoplay"autoplay"></video> 可能移动端有移动端的策略&#xff0c;但解决夸克…

基于LoRa无线数据传输的温湿度监测预警系统解决方案

为了维护仓储物品的品质&#xff0c;创造适宜的存储环境&#xff0c;就需要实时监测环境的温湿度信息&#xff0c;一旦温湿度出现异常就需要及时调整控制&#xff0c;从而保证品质稳定也能避免损失。 物通博联提供了软硬件一体的工业物联网解决方案&#xff0c;基于温湿度监测…

Python用来处理图像几何变换

Python用来处理图像几何变换的方法供大家学习和参考。 1. [代码][Python]代码 import Imagetry:imImage.open(test.jpg)#out im.resize((128, 128)) #改变大小#out im.rotate(45) #45旋转#out im.transpose(Image.FLIP_LEFT_RIGHT) #水平翻转#out im.transpose(Image.FLI…

Segment anything(图片分割大模型)

目录 1.Segment anything 2.补充图像分割和目标检测的区别 1.Segment anything 定义&#xff1a;图像分割通用大模型 延深&#xff1a;可以预计视觉检测大模型&#xff0c;也快了。 进一步理解&#xff1a;传统图像分割对于下图处理时&#xff0c;识别房子的是识别房子的模型…
推荐文章