北京国互网网站建设公司wordpress 屏蔽中文

张小明 2025/12/31 21:07:07
北京国互网网站建设公司,wordpress 屏蔽中文,wordpress主题 seo,十堰响应式网站模式匹配#xff0c;增强版的 switch 可以匹配各种类型#xff08;不只是整数或字符串#xff09;能解构复杂的数据结构#xff08;比如元组、枚举、结构体#xff09;是表达式#xff0c;有返回值要求穷尽所有可能#xff08;不会漏掉情况#xff09; 语法规则 mat…模式匹配增强版的 switch可以匹配各种类型不只是整数或字符串能解构复杂的数据结构比如元组、枚举、结构体是表达式有返回值要求穷尽所有可能不会漏掉情况语法规则match 要匹配的值 { 模式1 {...}, 模式2 {...}, 模式3 {...}, ... }✅ 整数匹配#[test] fn match_script() { let number 5; match number { 1 println!(1111), 2 println!(2222), 3..7 println!(闭区间), // 3 4 5 6 7 9..13 println!(半开区间), // 9 10 11 12 _ println!(default), // 通配符匹配其余所有 } }_表示通配符匹配其余所有3..73..7区间写法✅ 字符切片匹配#[test] fn match_script() { let input yes; // str 可拷贝类型 match input { yes println!(Affirmative!), no println!(Negative!), _ println!(Unknown response), } println!(input: {}, input); // 可正常输出 }和整数匹配一样匹配完所有权也还在✅ 枚举匹配#[derive(Debug)] enum Rgb { Red, Green, Blue, } #[test] fn match_script() { let color Rgb::Red; match color { Rgb::Red println!(Red), Rgb::Green println!(Green), Rgb::Blue println!(Blue), } }这里会打印Redmatch要求匹配的值每个可能的case都要实现硬性规定这样写会报错let color Rgb::Red; match color { Rgb::Red println!(Red), Rgb::Green println!(Green), }✅ Option 枚举匹配业务场景: 通常用于获取内容比如数据库查询只会有两个结果一个是值内容一个是无内容#[test] fn match_script() { let maybe_value: Optioni32 Some(41); match maybe_value { Some(x) println!(Got a value: {}, x), None println!(No value), } let maybe_none: Optioni32 None; match maybe_none { Some(x) println!(Got a value: {}, x), None println!(No value), } }outputGot a value: 41 No value枚举匹配会将变体的值带入例如Some(x) println!(Got a value: {}, x),匹配上就是Some(41), 后面的相当于函数实现x 相当于实参✅ Result 枚举匹配业务场景: 通常用于结果资源比如数据库连接只会有两个结果一个是连接成功一个是连接错误use std::fs::File; #[test] fn match_script() { let maybe_value: Resultstr, str Ok(successful); match maybe_value { Ok(x) println!(Got a value: {}, x), Err(e) println!(Error : {}, e), } // 打开文件 match File::open(Cargo.lock) { Ok(_file) println!(Opened file successfully), Err(error) println!(Error opening file: {}, error), } }outputGot a value: successful Opened file successfullyResult 和 Option 枚举匹配用法一样_file表示这个变量暂时没用到✅ 结构形式匹配✅ 解构元组#[test] fn match_script() { let maybe_tuple (0, msg, data); match maybe_tuple { (0, x, data) { println!(tuple.code: {:?}, x); } (0, msg, data_list) { println!(tuple is {:?}, maybe_tuple); } (0, x, y) println!(tuple.msg: {:?} data: {:?} , x, y), _ println!(tuple is not match), } }outputtuple.code: msgmatch要求匹配的各种可能性都要实现如果不想写那么多最后加一个_通配默认实现即可匹配中也支持具体值匹配例如(0, msg, data_list)和(0, x, y)都是可以匹配上的匹配参数支持混合(0, x, data)(z, x, y)最终输出的是(0, x, data)分支而不是(0, x, y)优先匹配上面的代码还是原来的代码现在换个case顺序#[test] fn match_script() { let maybe_tuple (0, msg, data); match maybe_tuple { (0, x, y) println!(tuple.msg: {:?} data: {:?} , x, y), (0, x, data) { println!(tuple.code: {:?}, x); } (0, msg, data_list) { println!(tuple is {:?}, maybe_tuple); } _ println!(tuple is not match), } }这种编译就通不过了第一个 case 已经涵盖了第二个 case代码冗余了✅ 解构结构体struct Point { x: i32, y: i32, } #[test] fn match_script() { let p Point { x: 10, y: 10 }; match p { Point { x: 0, y } println!(match Point.y: {}, y), Point { x, y } println!(Point: ({}, {}), x, y), } }outputPoint: (10, 10)这里我的Point { x, y }已经是所有可能性实现了再加_ println!(match _),就会报错了✅ 使用守卫在 case 中添加 if 语句match variable { val if 条件 {...} }#[test] fn match_script() { let num 7; match num { n if n 5 println!({} is less than 5, n), n if n % 2 0 println!({} is even and 5, n), _ println!({} is odd and 5, num), } }output7 is odd and 5✅ 引用匹配#[test] fn match_script() { let s String::from(hello); match s[..] { hello println!(Greeting!), _ println!(Something else), } // s 仍然可用 println!(s is still alive: {}, s); }outputGreeting! s is still alive: hellos[…] 有两层意思[..]为范围索引取值语法[a..b],a起始索引包含默认 0b结束索引不包含默认值为类型的长度…省略 start 和 end 时代表 “从开头到结尾” 的全范围。s在这里作用为 String - str 类型match 匹配 String先转化为字符切片与 case 数据类型一致这是常规写法在这里demo中match s[..]等价于match s再来看一个示例#[test] fn match_script() { let x Some(5); match x { Some(val) println!(x is {}, val), None println!(None), } println!(x is alive: {}, x.unwrap()); let y Some(6); match y { Some(val) println!(y is {}, val), None println!(None), } println!(y is alive: {}, y.unwrap()); }outputx is 5 x is alive: 5 y is 6 y is alive: 6x y 都正常输出了通过x.as_ref()借用值使用自然没问题后续所有权依旧可以使用但是 5 是可拷贝类型Some(5)是枚举啊这里有个规则枚举的所有变体的所有关联数据都实现了 Copy 时枚举才能派生 / 自动实现 CopyOption 里只有 Some 变体关联了 TSome(5) 也就意味着该 Option 实现了可 Copypub enum OptionT { None, Some(T), }换成下面这种写法就会报错String 不是可拷贝类型#[test] fn match_script() { let x Some(String::from(hello)); match x { Some(val) println!(x is {}, val), None println!(None), } println!(x is alive: {}, x.unwrap()); // x 的所有权已经给了 match如果此处要运行就需要改成 match x }✅ 多条件匹配#[test] fn match_script() { let day 6; match day { 1 | 2 | 3 | 4 | 5 println!(Weekday), 6 | 7 println!(Weekend), _ println!(Invalid day), } }一笔带过✅ 返回值#[test] fn match_script() { let status_code 404; let message match status_code { 200 OK, 404 Not Found, 500 Internal Server Error, _ Unknown, }; println!(Status: {}, message); }outputStatus: Not Foundmatch 不仅仅起 switch 的作用还有返回值可以用如果有变量接收当返回值用时每个 case 就要有 return 了(上面例子单一字符川也是return)let message match status_code { 200 OK, 404 println!(Not Found), 500 Internal Server Error, _ Unknown, };这种编译不过的✅ Vec/数组匹配#[test] fn match_script() { let arr [1, 2, 8]; match arr { [1, 2, 3] println!(Exact match), [x, y, ..] println!(First: {}, Second: {}, x, y), } }outputFirst: 1, Second: 2匹配向量和数组长度必须是固定的比方说你 case 里面写[1, 2, 3, 8]就会报错[x, y, ..]这种写法会通配匹配x, y为匹配的向量前两个值后面的..为通配作用这个..在一个 case模式 中只允许出现1次位置随意[.., y, z][x, .., z]✅ 写法变种if letif 写法式的 match这种方式通常用于业务中只处理一种模式时起 if 作用逻辑#[test] fn match_script() { let optional_target Some(rustlings); if let Some(word) optional_target { println!(match {}, word) } // ...... }outputmatch rustlings等价于下面写法相当于省去了所有模式的实现let optional_targets Some(rustlings); match optional_targets { Some(word) println!(match {}, word), None {} } // ......while letwhile 写法式的 match常用于重复从某个可变结构中取出值直到不满足某种模式为止。非常用于数组向量、元组这类复合数据类型循环遍历#[test] fn match_script() { let mut optional_integers: VecOptioni32 vec![Some(3), Some(2), Some(1)]; let mut cursor 1; while let Some(Some(n)) optional_integers.pop() { assert_eq!(n, cursor); cursor 1; } println!(✅ while let works!); }output✅ while let works!这里本质是将 while 与 match 的语法结合起来pop方法是出栈注意哈pop的返回值也是一个OptionT,这里在取值时要解析两层 OptionSome(Some(n))pop出完栈就没了组合变量声明表达式(let xxx)这种写法形成了一个有限的 while 循环等价于下面代码#[test] fn match_script() { let mut optional_integers: VecOptioni32 vec![Some(3), Some(2), Some(1)]; let mut cursor 1; loop { match optional_integers.pop() { Some(Some(n)) { assert_eq!(n, cursor); cursor 1; } Some(None) {} None break, // 向量所有值取完了 } } println!(✅ while let works!); }
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

建设官方网站e路护航简单易做的网站

当手机实现直连卫星的通信突破,当新能源汽车续航里程迈入千公里时代,当8K电视呈现纤毫毕现的画质,这些科技进步的背后,都离不开电子元器件的微观革新。作为电子产业的“细胞单元”,元器件的性能边界决定着终端产品的体…

张小明 2025/12/28 19:07:06 网站建设

如何做一家类似携程的网站wordpress用多大的带宽

5分钟快速上手Feathr:企业级特征工程的终极入门指南 【免费下载链接】feathr Feathr – A scalable, unified data and AI engineering platform for enterprise 项目地址: https://gitcode.com/gh_mirrors/fe/feathr 还在为复杂的特征工程平台配置而头疼吗&…

张小明 2025/12/29 22:02:30 网站建设

阿里巴巴网站推广怎么做网站页面设计大小

Hikari-LLVM15代码混淆快速上手:为你的代码穿上安全铠甲 【免费下载链接】Hikari-LLVM15 项目地址: https://gitcode.com/GitHub_Trending/hi/Hikari-LLVM15 你是否担心自己的代码被轻易逆向分析?是否想要为重要算法添加一层"代码迷彩"…

张小明 2025/12/28 22:06:23 网站建设

网站建设知识点的总结html5个人网页代码大全

如何快速使用ChromePass:完整的Chrome浏览器密码提取指南 【免费下载链接】chromepass Get all passwords stored by Chrome on WINDOWS. 项目地址: https://gitcode.com/gh_mirrors/chr/chromepass ChromePass是一款功能强大的开源工具,专门用于…

张小明 2025/12/28 16:12:53 网站建设

做刷单网站犯法吗网络推广的工作内容

Axure中文包终极指南:3步搞定全中文界面 【免费下载链接】axure-cn Chinese language file for Axure RP. Axure RP 简体中文语言包,不定期更新。支持 Axure 9、Axure 10。 项目地址: https://gitcode.com/gh_mirrors/ax/axure-cn 还在为Axure RP…

张小明 2025/12/28 7:02:56 网站建设

烟台做网站优化wp rocket wordpress

Sketch MeaXure:设计团队必备的5大标注效率提升方案 【免费下载链接】sketch-meaxure 项目地址: https://gitcode.com/gh_mirrors/sk/sketch-meaxure 在当今快节奏的设计开发流程中,设计标注已成为连接创意与实现的关键环节。传统标注方式不仅耗…

张小明 2025/12/28 22:12:07 网站建设