🔮
Rust学习笔记
  • 介绍
  • Rust入门
    • 安装使用
    • Cargo初用
    • 其他工具
    • 实例1
    • vim安装及配置
    • rust analyzer速度慢
  • rust基础
    • 关键字
    • 模块module和测试
    • 变量-数据类型
      • 浮点数
      • NaN
      • 有理数和复数
    • 单元类型
    • 字符串-切片-数组-元组-hashpmap
    • 结构体和枚举
    • 控制语句
    • 模式匹配match
    • 函数-method 闭包 迭代器
    • File操作
  • Rust进阶
    • 所有权
      • 所有权基础
      • 如何让函数使用某个值,但不获得其所有权
      • 切片
    • Rust VS C
    • 函数指针
  • Q&A
    • Rust的宏
    • cargo expand
    • web server
    • cargo watch使用
    • lib分类
    • cargo源
  • 参考
    • 博客
    • 项目
  • rust内存泄露
  • 智能指针
  • Tauri类似electron的框架
  • Tokio包使用
  • Rust wasm介绍
  • 多线程
    • C语言
  • Golang语言
    • 项目
  • Java
  • Javascript
Powered by GitBook
On this page
  1. rust基础

模式匹配match

当你只要匹配一个条件,且忽略其他条件时就用 if let ,否则都用 match。

match

match 的匹配必须穷尽所有情况


#![allow(unused)]
fn main() {
match target {
    模式1 => 表达式1,
    模式2 => {
        语句1;
        语句2;
        表达式2
    },
    _ => 表达式3
}
}

let ip_str = match ip1 {
        IpAddr::Ipv4 => "127.0.0.1",
        _ => "::1",
    };
enum Action {
    Say(String),
    MoveTo(i32, i32),
    ChangeColorRGB(u16, u16, u16),
}

fn main() {
    let actions = [
        Action::Say("Hello Rust".to_string()),
        Action::MoveTo(1,2),
        Action::ChangeColorRGB(255,255,0),
    ];
    for action in actions {
        match action {
            Action::Say(s) => {
                println!("{}", s);
            },
            Action::MoveTo(x, y) => {
                println!("point from (0, 0) move to ({}, {})", x, y);
            },
            Action::ChangeColorRGB(r, g, _) => {
                println!("change color into '(r:{}, g:{}, b:0)', 'b' has been ignored",
                    r, g,
                );
            }
        }
    }
}
enum Direction {
    East,
    West,
    North,
    South,
}

fn main() {
    let dire = Direction::South;
    match dire {
        Direction::East => println!("East"),
        Direction::North | Direction::South => {
            println!("South or North");
        },
    };
}
// 错误 没有处理 Direction::West

使用 | 可以匹配多个值, 而使用 ..= 可以匹配一个闭区间的数值序列

match n {
        // 匹配一个单独的值
        1 => println!("One!"),
        // 使用 `|` 填空,不要使用 `..` 或 `..=`
        __ => println!("match 2 -> 5"),
        // 匹配一个闭区间的数值序列
        6..=10 => {
            println!("match 6 -> 10")
        },
        _ => {
            println!("match 11 -> +infinite")
        }
    }
    
// @ 操作符可以让我们将一个与模式相匹配的值绑定到新的变量上
Point { x: 0..=5, y: y@ (10 | 20 | 30) } => println!("On the y axis at {}", y),

// 使用 .. 忽略一部分值
match numbers {
        (first,..,last) => {
           assert_eq!(first, 2);
           assert_eq!(last, 2048);
        }
    }
Previous控制语句Next函数-method 闭包 迭代器

Last updated 2 years ago