目录

枚举类型 enum

定义和声明

例1:Color 枚举

例2:Direction 枚举

例3:Weekday 枚举

类C枚举 C-like

打印输出

强制转成整数

例1:Weekday 枚举

例2:HttpStatus 枚举

例3:Color 枚举

模式匹配

match 表达式

枚举方法


枚举类型 enum

枚举类型是一种用于表示一组有限数量相互关联的离散型数据,这些值可以是不同的整数、字符串或其他类型的对象。枚举体内的元素称作“成员”,rust语言中,枚举里面的成员,都可以被看作是结构体,当然枚举还能嵌套另一个枚举。

定义和声明

在Rust中,枚举类型可以通过关键字enum来定义:

enum MyEnum {Variant1,Variant2,Variant3,//...VariantN,}

enum是enumerate/enumeration的缩略词,MyEnum为自定义的枚举名

Variant即变量的意思,有的译作“变体”,我觉得还是沿用C/C++语言中的称呼“成员”比较恰当。

Variant数量N一定有限的,太多的变量堆砌没有意义;而且N>=2,无成员或单个成员也无意义。

通过使用枚举类型,可以为不同的情况定义一组有限的取值。这在需要表示多个可能状态或选项的情况下非常有用。以下是几个常见的枚举类型经典实例:

例1:Color 枚举

Color 枚举表示颜色的不同可能取值,比如红色、绿色和蓝色。

enum Color {Red,Green,Blue,}

例2:Direction 枚举

Direction 枚举表示方向的不同可能取值,比如上、下、左和右。

enum Direction {Up,Down,Left,Right,}

例3:Weekday 枚举

Weekday 枚举表示一周中的不同天,比如一周7天的枚举。

enum Weekday {Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,}

类C枚举 C-like

与C/C++语言中的枚举成员只允许是usigned int型常量,也称枚举常量。Rust中具有C风格的枚举,那么必须满足枚举里面的成员都是单元结构体

打印输出

使用#[derive(Debug)]简化输出,格式:println!(“{:” />::VariantN);

#[derive(Debug)]enum Number {Zero,One,Two,Three,}fn main() {println!("zero is {:?}", Number::Zero);println!("one is {:?}", Number::One);println!("Two is {:?}", Number::Two);println!("Three is {:?}", Number::Three);}

输出:

zero is Zero
one is One
Two is Two
Three is Three

强制转成整数

不使用#[derive(Debug)],强制转化为整数后输出

enum Number {Zero,One,Two,Three,}fn main() {println!("zero is {}", Number::Zero as i32);println!("one is {}", Number::One as i32);println!("Two is {}", Number::Two as i32);println!("Three is {}", Number::Three as i32);}

输出:

zero is 0
one is 1
Two is 2
Three is 3

类C枚举也可以通过赋值运算,变成一组离散的整数:

#![allow(dead_code)]#[derive(Debug)]enum Number {Zero,One,Two,Five = 5,Six,}fn main() {println!("zero is {}", Number::Zero as i32);println!("one is {}", Number::One as i32);println!("Five is {}", Number::Five as i32);println!("Six is {}", Number::Six as i32);}

输出:

0
1
5
6

注:在整个程序中枚举成员Two没被调用过,可以用#![allow(dead_code)]来滤去警告。没有此行,上述程序在编译执行能输出结果,但同时警告:warning: variant `Two` is never constructed。

例1:Weekday 枚举

Weekday 枚举通过赋值,使得星期一到星期日对应整数1~7;而非默认的星期日到星期六对应整数0~6。

#![allow(dead_code)]enum Weekday {Sunday = 7,Monday = 1,Tuesday,Wednesday,Thursday,Friday,Saturday,}fn main() {println!("Sunday is {}", Weekday::Sunday as i32);println!("Tuesday is {}", Weekday::Tuesday as i32);println!("Saturday is {}", Weekday::Saturday as i32);}

输出:

Sunday is 7
Tuesday is 2
Saturday is 6

例2:HttpStatus 枚举

HttpStatus 枚举表示不同的 HTTP 状态码,比如200表示成功,404表示找不到页面,500表示服务器内部错误等,共有近百个成员,列举部分如下:

#![allow(dead_code)] enum HttpStatus {Continue = 100, //"CONTINUE" 继续SwitchingProtocols, //"SWITCHING_PROTOCOLS" 切换协议Processing, //"PROCESSING" 执行Checkpoint, //"CHECKPOINT" 检查点OK = 200, //"OK" OKCREATED, //"Created" 创建ACCEPTED, //"Accepted" 接受NonAuthoritativeInformation, //"NON_AUTHORITATIVE_INFORMATION" 非权威信息NoContent, //"NO_CONTENT" 没有内容ResetContent, //"RESET_CONTENT" 重置内容PartialContent, //"PARTIAL_CONTENT" 部分内容MultiStatus, //"MULTI_STATUS" 多个状态AlreadyReported, //"ALREADY_REPORTED" 已发IMUsed = 226, //"IM_USED" 已过时MultipleChoices = 300, //"MULTIPLE_CHOICES" 多选择MovedPermanently, //"MOVED_PERMANENTLY" 永久移动FOUND, //"Found" 找到MovedTemporarily, //"MOVED_TEMPORARILY"SeeOther, //"SEE_OTHER" 参见其它NotModified, //"NOT_MODIFIED" 未修改UseProxy, //"USE_PROXY"),TemporaryRedirect, //"TEMPORARY_REDIRECT" 暂时重定向PermanentRedirect, //"PERMANENT_REDIRECT" 永久重定向//......BadRequest = 400, //"BAD_REQUEST" 错误请求Unauthorized, //"UNAUTHORIZED" 未经授权PaymentRequired, //"PAYMENT_REQUIRED" 付费请求Forbidden, //"FORBIDDEN" 禁止NotFound, //"NOT_FOUND" 未找到MethodNotAllowed, //"METHOD_NOT_ALLOWED" 方法不容许NotAcceptable, //"NOT_ACCEPTABLE" 方法不接受//......InternalServerError = 500, // "INTERNAL_SERVER_ERROR" 服务器遇到了意料不到的情况,不能完成客户的请求NotImplemented, //"NOT_IMPLEMENTED"服务器不支持实现请求所需要的功能BadGateway, //"BAD_GATEWAY" 从上游服务器接收到无效的响应ServiceUnavailable, //"SERVICE_UNAVAILABLE" 服务不可用GatewayTimeout, //"GATEWAY_TIMEOUT") 网关不能及时地从远程服务器获得应答//......}fn main() {println!("OK is {}", HttpStatus::OK as i32);println!("Bad Request is {}", HttpStatus::BadRequest as i32);}

输出:

OK is 200
Bad Request is 400

例3:Color 枚举

用{:06x}格式输出6位16进制数表示的颜色

enum Color {Red = 0xff0000,Green = 0x00ff00,Blue = 0x0000ff,}fn main() {println!("roses are #{:06x}", Color::Red as i32);println!("leaves are #{:06x}", Color::Green as i32);println!("violets are #{:06x}", Color::Blue as i32);println!("red is {}", Color::Red as i32);println!("green is {}", Color::Green as i32);println!("blue is {}", Color::Blue as i32);}

输出:

roses are #ff0000
leaves are #00ff00
violets are #0000ff
red is 16711680
green is 65280
blue is 255

模式匹配

枚举类型在 Rust 中经常与模式匹配(pattern matching)结合使用,以根据枚举类型的值执行不同的操作。

match 表达式

match 表达式可以用于对枚举变体进行模式匹配,并根据所匹配的变体执行相应的代码块。它的语法如下:

match value {pattern1 => {// 如果 value 匹配了 pattern1},pattern2 => {// 如果 value 匹配了 pattern2},// 其他模式...}

如定义一个名为 Color 的枚举类型,其中包含了三个成员:Red、Green 和 Blue。每个成员都是一个合法的标识符,用于表示不同的颜色。

enum Color {Red,Green,Blue,}

对上述的 Color 枚举类型,可以使用模式匹配来执行相应的操作:

fn print_color(color: Color) {match color {Color::Red => println!("The color is Red"),Color::Green => println!("The color is Green"),Color::Blue => println!("The color is Blue"),}}

上述示例中,函数 print_color 接收一个 Color 类型的参数,并使用 match 进行模式匹配。根据传入的颜色值,会执行相应的操作。

枚举方法

还可以自定义方法,为每个成员定义相应的值。

#![allow(dead_code)]enum Color {Red,Green,Blue,Yellow,Magenta,Cyan,}impl Color {fn to_rgb(&self) -> (u8, u8, u8) {match self {Color::Red => (255, 0, 0),Color::Green => (0, 255, 0),Color::Blue => (0, 0, 255),Color::Yellow => (255, 255, 0),Color::Magenta => (255, 0, 255),Color::Cyan => (0, 255, 255),}}}fn main() {let red = Color::Red;let red_rgb = red.to_rgb();println!("RGB value of red: {:?}", red_rgb);let yellow = Color::Yellow;let yellow_rgb = yellow.to_rgb();println!("RGB value of yellow: {:?}", yellow_rgb);}

在上述示例中,我们为 Color 枚举类型实现了一个 to_rgb 方法,用于将颜色转换为 RGB 值。

总结起来,Rust 的枚举类型提供了强大的模式匹配功能,使得枚举类型非常灵活和可靠,适用于各种场景和数据处理需求。本文只初步学习类C风格的枚举类型,其它分类且听下回分解。