Enums and Pattern Matching

摘要

枚举定义

enum IpAddrKind {
V4,
V6,
}

枚举方法

fn main() {
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
} impl Message {
fn call(&self) {
// method body would be defined here
}
} let m = Message::Write(String::from("hello"));
m.call();
}

Option<T>使用

match使用

Defining an Enum

枚举属性的类型统一为枚举的名称

enum IpAddrKind {
V4,
V6,
}
 let four = IpAddrKind::V4;
let six = IpAddrKind::V6;

Note that the variants of the enum are namespaced under its identifier, and we use a double colon to separate the two. The reason this is useful is that now both values IpAddrKind::V4 and IpAddrKind::V6 are of the same type: IpAddrKind. We can then, for instance, define a function that takes any IpAddrKind:

fn route(ip_kind: IpAddrKind) {}
route(IpAddrKind::V4);
route(IpAddrKind::V6);
    enum IpAddrKind {
V4,
V6,
} struct IpAddr {
kind: IpAddrKind,
address: String,
} let home = IpAddr {
kind: IpAddrKind::V4,
address: String::from("127.0.0.1"),
}; let loopback = IpAddr {
kind: IpAddrKind::V6,
address: String::from("::1"),
};
    enum IpAddr {
V4(String),
V6(String),
} let home = IpAddr::V4(String::from("127.0.0.1")); let loopback = IpAddr::V6(String::from("::1"));
fn main() {
enum IpAddr {
V4(u8, u8, u8, u8),
V6(String),
} let home = IpAddr::V4(127, 0, 0, 1); let loopback = IpAddr::V6(String::from("::1"));
}
#![allow(unused_variables)]
fn main() {
struct Ipv4Addr {
// --snip--
} struct Ipv6Addr {
// --snip--
} enum IpAddr {
V4(Ipv4Addr),
V6(Ipv6Addr),
}
}
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}

This enum has four variants with different types:

  • Quit has no data associated with it at all.
  • Move includes an anonymous struct inside it.
  • Write includes a single String.
  • ChangeColor includes three i32 values.
fn main() {
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
} impl Message {
fn call(&self) {
// method body would be defined here
}
} let m = Message::Write(String::from("hello"));
m.call();
}

[derive(Debug)] 会自动实现一个输出格式,然后就可以使用{:?}输出枚举

fn testEnum(){

    #[derive(Debug)]
enum Message {
Quit,
Move{x: i16, y: i16},
Write(String),
ChangeColor(i32,i32,i32),
} impl Message {
fn call(&self) {
let v1 = Message::Quit;
let v2 = Message::Move{x: 25, y: 25}; let v4 = Message::ChangeColor(100,100,100);
println!("{:#?} ",v1);
println!("{:#?} ",v2);
println!("{:?}",v4);
} } let m = Message::Write(String::from("enum show "));
println!("{:?}",m);
m.call();
}
Write("enum show ")
Quit
Move {
x: 25,
y: 25,
}
ChangeColor(100, 100, 100)

Rust枚举中一个超级重要的的特性:所有枚举都是同一类型,这一点在rust中的应用, 相当于Java的抽象、继承,相当于C++中的泛型,针对此特点,特举例如下

#[derive(Debug)]
enum WorkSort {
Student,
Teacher,
Doctor,
} impl WorkSort{
pub fn desc(&self){
println!("I am a {:?}",self);
}
} pub fn test1(){
let tch = WorkSort::Teacher;
tch.desc(); let std = WorkSort::Student;
std.desc(); }

调用test1方法,输出

I am a Teacher
I am a Student

The Option Enum and Its Advantages Over Null Values

Programming language design is often thought of in terms of which features you include, but the features you exclude are important too. Rust doesn’t have the null feature that many other languages have. Null is a value that means there is no value there. In languages with null, variables can always be in one of two states: null or not-null.

The problem isn’t really with the concept but with the particular implementation. As such, Rust does not have nulls, but it does have an enum that can encode the concept of a value being present or absent. This enum is Option<T>, and it is defined by the standard library as follows:

#![allow(unused_variables)]
fn main() {
enum Option<T> {
Some(T),
None,
}
}

The Option<T> enum is so useful that it’s even included in the prelude; you don’t need to bring it into scope explicitly. In addition, so are its variants: you can use Some and None directly without the Option:: prefix. The Option<T> enum is still just a regular enum, and Some(T) and None are still variants of type Option<T>.

For now, all you need to know is that <T> means the Some variant of the Option enum can hold one piece of data of any type. Here are some examples of using Option values to hold number types and string types:

fn main() {
let some_number = Some(5);
let some_string = Some("a string"); let absent_number: Option<i32> = None;
}

In short, because Option<T> and T (where T can be any type) are different types, the compiler won’t let us use an Option<T> value as if it were definitely a valid value. For example, this code won’t compile because it’s trying to add an i8 to an Option<i8>:

fn main() {
let x: i8 = 5;
let y: Option<i8> = Some(5); let sum = x + y;//error
}

The match Control Flow Operator

Rust has an extremely powerful control flow operator called match that allows you to compare a value against a series of patterns and then execute code based on which pattern matches. Patterns can be made up of literal values, variable names, wildcards, and many other things;
fn main() {
println!("----------match----------");
let coin = Coin::Penny;
let res = value_in_cents(coin);
println!("{}",res); let coin = Coin::Dime;
let res = value_in_cents(coin);
println!("{}",res);
} enum Coin {
Penny,
Nockel,
Dime,
Quarter,
} fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nockel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
$ cargo run
Compiling enum_match v0.1.0 (/opt/wks/rust_study/enum_match)
warning: variant is never constructed: `Nockel`
--> src/main.rs:15:5
|
15 | Nockel,
| ^^^^^^
|
= note: `#[warn(dead_code)]` on by default warning: variant is never constructed: `Quarter`
--> src/main.rs:17:5
|
17 | Quarter,
| ^^^^^^^ warning: 2 warnings emitted Finished dev [unoptimized + debuginfo] target(s) in 0.52s
Running `target/debug/enum_match`
----------match----------
1
10
#[derive(Debug)]
enum UsState {
Alabama,
Alaska,
// --snip--
} enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
} fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("State quarter from {:?}!", state);
25
}
}
} fn main() {
value_in_cents(Coin::Quarter(UsState::Alaska));
}
Option<T> match
#[derive(Debug)]
enum Option<T> {
None,
Some(T)
} fn plus_one(x: Option<i32>) -> Option<i32>{
match x {
Option::None => Option::None,
Option::Some(i) => Option::Some(i+1),
}
} fn test_match(){
let five = Option::Some(5);
let six = plus_one(five);
println!("{:?}",six);
let none = plus_one(Option::None);
println!("{:?}",none);
}

Matches Are Exhaustive

fn main() {
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
Some(i) => Some(i + 1),
}
} let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);
}

We didn’t handle the None case, so this code will cause a bug. Luckily, it’s a bug Rust knows how to catch. If we try to compile this code, we’ll get this error:

$ 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 error: aborting due to previous error For more information about this error, try `rustc --explain E0004`.
error: could not compile `enums`. To learn more, run the command again with --verbose.

The _ Placeholder

Rust also has a pattern we can use when we don’t want to list all possible values. For example, a u8 can have valid values of 0 through 255. If we only care about the values 1, 3, 5, and 7, we don’t want to have to list out 0, 2, 4, 6, 8, 9 all the way up to 255. Fortunately, we don’t have to: we can use the special pattern _ instead:

fn main() {
let some_u8_value = 0u8;
match some_u8_value {
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
_ => (),
}
}
_ => (),
这一行表示轮空,什么也不会输出

The _ pattern will match any value. By putting it after our other arms, the _ will match all the possible cases that aren’t specified before it. The () is just the unit value, so nothing will happen in the _ case. As a result, we can say that we want to do nothing for all the possible values that we don’t list before the _ placeholder.

However, the match expression can be a bit wordy in a situation in which we care about only one of the cases. For this situation, Rust provides if let.

Concise Control flow with if let

#![allow(unused_variables)]
fn main() {
println!("Hello, match!");
let1();
let2();
} #[derive(Debug)]
enum Option<T> {
Some(T),
None
} fn let1(){
let some_u3_value = Option::Some(0u8);
match some_u3_value {
Option::Some(3) => println!("three"),
_ => (),
} if let Option::Some(3) = some_u3_value{
println!("three");
} } fn let2(){
let some_u3_value = Option::Some(0u8);
let mut count = 0;
match some_u3_value {
Option::Some(3) => println!("three"),
_ => count += 1,
} if let Option::Some(3) = some_u3_value{
println!("three");
}else{
count += 1;
}
println!("{}",count);
}

2.8 rust 枚举与模式匹配的更多相关文章

  1. Rust中的枚举及模式匹配

    这个enum的用法,比c要强,和GO类似. enum Coin { Penny, Nickel, Dime, Quarter, } fn value_in_cents(coin: Coin) -> ...

  2. 学习Rust第一天 Rust语言特点

    学习Rust之前,我觉得应该首先了解Rust语言的设计目的是什么?为什么会诞生这门语言?这门语言和其他的语言有什么不同. Rust语言的设计特点 高性能:rust拥有和C++相近的性能表现,所以在嵌入 ...

  3. Swift 模式匹配

    前言 在 Swift 中模式匹配是个重要的概念. 最常用的模式匹配是 switch 语法. 模式匹配非常灵活,在使用 switch 进行一轮模式匹配时,不需要所有的 case 都是同一种风格. let ...

  4. swift 模式

    原文:http://www.cocoachina.com/newbie/basic/2014/0612/8800.html 模式(pattern)代表了单个值或者复合值的结构.比如,元组(1, 2)的 ...

  5. ballerina 学习十五 控制流

    ballerina 的控制流没有什么特殊,只是相比一般语言多了一个模式匹配的操作match ,实际上其他语言(erlang elixir rust 中的模式匹配是很强大的) 简单例子 if/else ...

  6. rust Option枚举

    枚举 1 fn main() { 2 let a_binding; 3 { 4 let x = 2; 5 a_binding = x * x; 6 } 7 println!("a bindi ...

  7. 专访Rust——由Mozilla开发的系统编程语言(目标人群就是那些纠结的C++程序员,甚至也是他们自己)

    Rust是由Mozilla开发的专门用来编写高性能应用程序的系统编程语言.以下是对Rust的创始人——Graydon Hoare的采访. Graydon Hoare,自称为职业编程语言工程师,从200 ...

  8. rust 高级话题

    目录 rust高级话题 前言 零大小类型ZST 动态大小类型DST 正确的安装方法 结构体 复制和移动 特征对象 引用.生命周期.所有权 生命周期 错误处理 交叉编译 智能指针 闭包 动态分派和静态分 ...

  9. rust语法

    目录 rust语法 前言 一.数据类型 1.1 标量scalar 1.2 复合compound 1.3 切片slice 1.4 引用(借用)reference 1.5 智能指针smart pointe ...

随机推荐

  1. Calendar.set方法获取前一天的当前时刻

    获取前几天的当前时刻的时间方法 Calendar cal = Calendar.getInstance(); Date date = new Date();// 获取当前时间 cal.setTime( ...

  2. 跟着老猫来搞GO-容器(1)

    前期回顾 前面的一章主要和大家分享了GO语言的函数的定义,以及GO语言中的指针的简单用法,那么本章,老猫就和大家一起来学习一下GO语言中的容器. 数组 数组的定义 说到容器,大家有编程经验的肯定第一个 ...

  3. Centos8 部署 ElasticSearch 集群并搭建 ELK,基于Logstash同步MySQL数据到ElasticSearch

    Centos8安装Docker 1.更新一下yum [root@VM-24-9-centos ~]# yum -y update 2.安装containerd.io # centos8默认使用podm ...

  4. python实现开闭操作

    目录: 开闭操作的作用 (一)开操作 (二)闭操作 (三)开操作完成其他任务 (1)提取水平垂直线 (2)消除干扰线 (3)提取满足要求的形状 开闭操作的作用: (一)开操作(特点作用:消除噪点--- ...

  5. [hdu7078]Pty with card

    显然问题被分为两部分,先考虑如何求$F(n)$-- 令第一次所选的人编号为1,其之后所有人按顺时针依次编号为$2,3,...,n$,那么用一个序列来描述状态,其中第$i$个元素为当前存在的人中编号第$ ...

  6. [gym102220I]Temperature Survey

    (为了方便,以下记$a_{0}=0,a_{n+1}=n$​​,并将$n$​​加上1) 构造一个$n$行的网格图,从上到下第$i$行有$a_{i}$个格子,格子左对齐 记第$i$行第$j$个格子为$(i ...

  7. [spojRNG]Random Number Generator

    先将所有数加上Ri,即变为区间[0,2Ri],考虑容斥,将区间容斥为[0,+oo)-[2Ri,+oo),然后对[2Ri,+oo)令$bi=ai-2Ri$,相当于范围都是[0,+oo)问题转化为求n个正 ...

  8. 青龙+Nvjdc短信登陆对接Xdd-plus推送+Ninja CK登陆教程(11.23更新)

    一.准备工作 1.shh工具(powshell.gitbash等等) 2.购买一台云服务器(阿里云.腾讯云都可以) 3.安装宝塔面板 宝塔Linux面板安装教程 - 2021年8月18日更新 - 7. ...

  9. WebGoat8.2.2-A8不安全的反序列化

    1.概念   使用反序列化在各编程语言中略有不同,如Java.PHP.Python.Ruby.C/C++,但在关键概念上是一样的     序列化:将(内存中的)对象转化成数据格式,以便存储或传输   ...

  10. 洛谷 P6499 - [COCI2016-2017#2] Burza(状压 dp)

    题面传送门 一道挺有意思的思维题(?) 首先我们假设根节点深度为 \(0\),那么 Daniel 的目标显然就是堵住一些节点使得 Stjepan 不能移动到深度为 \(k\) 的节点,Stjepan ...