Rust的enum枚举的强大用法

在Rust中,enum(枚举)是一种非常强大的类型,它可以包含多个变体(variants),每个变体可以是不同的类型,包括复杂类型。这使得enum在Rust中不仅用于表示简单的状态或选项集合,还可以用于表示更复杂的数据结构。

以下是一个具体的例子,展示了Rust中enum如何包含复杂类型:

rust 复制代码
// 定义一个结构体,表示一个点(Point)
#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
}

// 定义一个枚举,表示不同的形状(Shape)
#[derive(Debug)]
enum Shape {
    Circle {
        radius: i32,
        center: Point, // 复杂类型:包含一个Point结构体
    },
    Rectangle {
        width: i32,
        height: i32,
        top_left: Point, // 复杂类型:包含一个Point结构体
    },
    // 可以添加更多变体,例如三角形等,每个都可以有自己的字段
}

fn main() {
    // 创建Circle形状的实例
    let circle = Shape::Circle {
        radius: 10,
        center: Point { x: 0, y: 0 },
    };

    // 创建Rectangle形状的实例
    let rectangle = Shape::Rectangle {
        width: 20,
        height: 10,
        top_left: Point { x: -10, y: -5 }, // 矩形左上角的位置
    };

    // 打印形状信息
    println!("{:?}", circle);
    println!("{:?}", rectangle);

    // 匹配形状并执行不同操作
    match circle {
        Shape::Circle { radius, center } => {
            println!("Circle with radius {} and center at ({}, {})", radius, center.x, center.y);
        },
        Shape::Rectangle { width, height, top_left } => {
            // 这里不会匹配到,因为circle是Circle变体
            // 但为了完整性,我们仍然提供这个分支
            println!("Rectangle with width {}, height {}, and top-left at ({}, {})", width, height, top_left.x, top_left.y);
        },
        // 可以添加更多变体匹配分支(尽管在这个例子中不需要)
    }

    match rectangle {
        Shape::Circle { .. } => {
            // 这里不会匹配到,因为rectangle是Rectangle变体
        },
        Shape::Rectangle { width, height, top_left } => {
            println!("Matched Rectangle: width = {}, height = {}, top-left = ({}, {})", width, height, top_left.x, top_left.y);
        },
    }
}

在这个例子中,Shape枚举有两个变体:CircleRectangle。每个变体都有自己的字段,这些字段可以是基本类型(如i32)或复杂类型(如Point结构体)。在main函数中,我们创建了Shape枚举的两个实例:一个Circle和一个Rectangle,并打印了它们的信息。然后,我们使用match表达式来匹配这些形状并执行不同的操作。

另外还可以对该枚举类型进行方法实现扩展:

rust 复制代码
enum Shape {
    Circle {
        radius: i32,
        center: Point, // 复杂类型:包含一个Point结构体
    },
    Rectangle {
        width: i32,
        height: i32,
        top_left: Point, // 复杂类型:包含一个Point结构体
    },
    // 可以添加更多变体,例如三角形等,每个都可以有自己的字段
}

impl Shape {
	pub fn area(&self) -> f64 {
		match self {
			Shape::Rectangle { width, height } => (width as f64) * (height as f64),  
            Shape::Circle { radius } => std::f64::consts::PI * (radius as f64).powi(2), 
		}
}

这个例子展示了Rust中enum如何能够包含复杂类型,并展示了如何使用这些复杂类型来进行模式匹配和数据处理。

相关推荐
再见晴天*_*3 小时前
SpringBoot 中单独一个类中运行main方法报错:找不到或无法加载主类
java·开发语言·intellij idea
lqjun08274 小时前
Qt程序单独运行报错问题
开发语言·qt
ftpeak4 小时前
从零开始使用 axum-server 构建 HTTP/HTTPS 服务
网络·http·https·rust·web·web app
咸甜适中5 小时前
rust语言 (1.88) 学习笔记:客户端和服务器端同在一个项目中
笔记·学习·rust
hdsoft_huge6 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘6 小时前
39.网络流入门
开发语言·网络·c++·算法
未来之窗软件服务7 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
小冯记录编程7 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
1uther7 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
C_Liu_8 小时前
C++:类和对象(下)
开发语言·c++