会员登录 - 用户注册 - 设为首页 - 加入收藏 - 网站地图 Rust语言:枚举与match表达式!

Rust语言:枚举与match表达式

时间:2024-05-12 09:03:48 来源:晨蓝头条 作者:时尚 阅读:383次

    枚举在Rust里是枚举很重要的一种类型,因为它体现了Rust的达式一个重要设计:必须处理空值和错误。先看普通的枚举枚举enum IpAddrKind {

     V4,

     V6,

    }

    let four = IpAddrKind::V4;

    let six = IpAddrKind::V6;这里的V4和V6称为IpAddrKind的变体。C的达式枚举实际上是整型,但前面说过Rust是枚举强类型语言,类型互转是达式很困难的。每个enum变体都可以选择绑定一个数据,枚举这个设计有什么用呢?enum Message {

     Quit,达式

     Move { x: i32, y: i32 },

     Write(String),

     ChangeColor(i32, i32, i32),

    }内置的Option枚举enum Option {

     Some(T),

     None,

    }Option表示的是,可能有值也可能没有值的枚举情况,如果有值,达式这个值在Some里,枚举如果没有值,达式那就是枚举None十亿美元的错误In his 2009 presentation “Null References: The Billion Dollar Mistake,” Tony Hoare, the inventor of null, has this to say:I call it my billion-dollar mistake. At that time, I was designing the first comprehensive type system for references in an object-oriented language.My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.如果一个表达式返回了Option类型,要想从里面获取结果,达式就必须要处理空值情况,枚举否则编译器就会报错。fn plus_one(x: Option) -> Option {

     match x {

     Some(i) => Some(i + 1),

     }

    }$ cargo run

     Compiling enums v0.1.0 (file:///projects/enums)

    error[E0004]: non-exhaustive patterns: `None` not covered

     --> src/main.rs:3:15

     |

    3 | match x {

     | ^ pattern `None` not covered

     |

     = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

     = note: the matched value is of type `Option`

    

    error: aborting due to previous error内置的Result枚举Result用于返回一个结果,但也有可能出错的情况,使用枚举,就要求调用方要显式地处理错误。enum Result {

     Ok(T),

     Err(E),

    }比如打开一个文件use std::fs::File;

    

    fn main() {

     let f: u32 = File::open("hello.txt");

    }可是没有输出令人兴奋的结果$ cargo run

     Compiling error-handling v0.1.0 (file:///projects/error-handling)

    error[E0308]: mismatched types

     --> src/main.rs:4:18

     |

    4 | let f: u32 = File::open("hello.txt");

     | --- ^^^^^^^^^^^^^^^^^^^^^^^ expected `u32`, found enum `std::result::Result`

     | |

     | expected due to this

     |

     = note: expected type `u32`

     found enum `std::result::Result`因为类型不对。标准库返回的是Result类型,必须显式地处理成功和错误两种情况。Rust的难学之一体现在不断地与编译器博斗使代码编译通过,这也是可以理解的。因为Rust把很多在运行时检查的内容提前到了编译阶段,而当编译器不能推断出确定的规则时,只能要求编码者提供更具体的信息,以帮助编译器判断代码的问题。用Rust的感觉就像侍奉编译器的感觉。也由于这个原因,Rust的编译速度相对较慢。尽管如此,这仍然是Rust让人兴奋的一个地方,因为定位一个运行中程序bug的成本要远高于成功编译一个健康的程序,更多的时候,我们希望编译器能做得更多,而不是更少。

(责任编辑:热点)

相关内容
  • 中金:猪价或启动上行 猪企资产负债表修复进入关键期
  • 当你老了,语言也会衰老吗?
  • 韩国研究所称吃泡菜减轻新冠症状:常吃泡菜的国家新冠患者死亡率较低
  • 名家笔下的年味
  • 瓜帅:这是三场决赛全赢就夺冠 不能指望对手丢分
  • 十三届全国人大四次会议举行新闻发布会
  • 快查收!这是份春季星空密码
  • 财政部办理建议提案办结率100%
推荐内容
  • 王者荣耀赛场再现“成渝大战” 成都AG取胜拿到总决赛门票
  • H&M恶意抹黑新疆棉花,我们该怎么维护自身权益?
  • 金面具的眉毛为何备受关注?
  • 2020年公安机关315名民警因公牺牲
  • 超八成北交所公司一季度实现盈利
  • 身边科学:化解“另一场全球危机”