【Rust练习】15.match 和 if let

练习题来自:https://practice-zh.course.rs/pattern-match/match-iflet.html

1

rust 复制代码
// 填空
enum Direction {
    East,
    West,
    North,
    South,
}

fn main() {
    let dire = Direction::South;
    match dire {
        Direction::East => println!("East"),
        __  => { // 在这里匹配 South 或 North
            println!("South or North");
        },
        _ => println!(__),
    };
}

答案:

rust 复制代码
fn main() {
    let dire = Direction::South;
    match dire {
        Direction::East => println!("East"),
        Direction::North | Direction::South => {
            println!("South or North");
        },
        _ => println!("West"),
    };
}

2 🌟🌟 match 是一个表达式,因此可以用在赋值语句中

rust 复制代码
fn main() {
    let boolean = true;

    // 使用 match 表达式填空,并满足以下条件
    //
    // boolean = true => binary = 1
    // boolean = false => binary = 0
    let binary = __;

    assert_eq!(binary, 1);
}

答案

rust 复制代码
fn main() {
    let boolean = true;
    
    let binary = match boolean {
        true => 1,
        false => 2,
    };

    assert_eq!(binary, 1);
}

3 🌟🌟 使用 match 匹配出枚举成员持有的值

rust 复制代码
// 填空
enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(i32, i32, i32),
}

fn main() {
    let msgs = [
        Message::Quit,
        Message::Move{x:1, y:3},
        Message::ChangeColor(255,255,0)
    ];

    for msg in msgs {
        show_message(msg)
    }
} 

fn show_message(msg: Message) {
    match msg {
        __ => { // 这里匹配 Message::Move
            assert_eq!(a, 1);
            assert_eq!(b, 3);
        },
        Message::ChangeColor(_, g, b) => {
            assert_eq!(g, __);
            assert_eq!(b, __);
        }
        __ => println!("no data in these variants")
    }
}

枚举的加入真是把match完全玩活了,和其他语言的switch case完全不一样了。C++实现类似的功能,我觉得应该需要多态的辅助。不过本质上将多个不同类型放在一起匹配的意义也很有限。

rust 复制代码
fn show_message(msg: Message) {
    match msg {
        Message::Move { x:a, y:b } => { // 这里匹配 Message::Move
            assert_eq!(a, 1);
            assert_eq!(b, 3);
        },
        Message::ChangeColor(_, g, b) => {
            assert_eq!(g, 255);
            assert_eq!(b, 0);
        }
        __ => println!("no data in these variants")
    }
}

4

rust 复制代码
fn main() {
    let alphabets = ['a', 'E', 'Z', '0', 'x', '9' , 'Y'];

    // 使用 `matches` 填空
    for ab in alphabets {
        assert!(__)
    }
} 

这里的matches!类似于将match的条件仅限制为一个,成功与否返回布尔值。

rust 复制代码
fn main() {
    let alphabets = ['a', 'E', 'Z', '0', 'x', '9' , 'Y'];

    // 使用 `matches` 填空
    for ab in alphabets {
        assert!(matches!(ab, 'A'..='Z' | 'a'..='z'| '0'..='9'))
    }
} 

5

rust 复制代码
enum MyEnum {
    Foo,
    Bar
}

fn main() {
    let mut count = 0;

    let v = vec![MyEnum::Foo,MyEnum::Bar,MyEnum::Foo];
    for e in v {
        if e == MyEnum::Foo { // 修复错误,只能修改本行代码
            count += 1;
        }
    }

    assert_eq!(count, 2);
}

这里展示出来的matches!又类似于等号对枚举的重载,这次我站C++,明显是等号更直观,就算Rust不支持重载基本符号,加个语法糖也行啊。

rust 复制代码
fn main() {
    let mut count = 0;

    let v = vec![MyEnum::Foo,MyEnum::Bar,MyEnum::Foo];
    for e in v {
        if matches!(e, MyEnum::Foo) { // 修复错误,只能修改本行代码
            count += 1;
        }
    }

    assert_eq!(count, 2);
}

6

rust 复制代码
fn main() {
    let o = Some(7);

    // 移除整个 `match` 语句块,使用 `if let` 替代
    match o {
        Some(i) => {
            println!("This is a really long string and `{:?}`", i);
        }
        _ => {}
    };
}

这种if语法有点奇怪,按理说if的条件判断都应该用双等于号才对,但是理解为赋值就好了。

rust 复制代码
fn main() {
    let o = Some(7);
    
    if let Some(i) = o {
        println!("This is a really long string and `{:?}`", i);
    }
}

7

rust 复制代码
// 填空
enum Foo {
    Bar(u8)
}

fn main() {
    let a = Foo::Bar(1);

    __ {
        println!("foobar 持有的值是: {}", i);
    }
}

由于Foo中只有一个枚举值,if匹配百分百会命中。

rust 复制代码
fn main() {
    let a = Foo::Bar(1);

    if let Foo::Bar(i) = a {
        println!("foobar 持有的值是: {}", i);
    }
}

8

rust 复制代码
enum Foo {
    Bar,
    Baz,
    Qux(u32)
}

fn main() {
    let a = Foo::Qux(10);

    // 移除以下代码,使用 `match` 代替
    if let Foo::Bar = a {
        println!("match foo::bar")
    } else if let Foo::Baz = a {
        println!("match foo::baz")
    } else {
        println!("match others")
    }
}

使用match时,注意default匹配。

rust 复制代码
fn main() {
    let a = Foo::Qux(10);

    match a {
        Foo::Bar => println!("match foo::bar"),
        Foo::Baz => println!("match foo::baz"),
        _ => println!("match others"),
    }
}

题外话,如果你使用IDE自带的format对代码进行格式化,任何列表匹配的最后一项都会自动加一个顿号。C++的枚举类也支持这个功能,但其他的语言结构和其他语言我就不清楚了。这么设计的原因是,git对代码变更统计是以行为单位的,如果最后一个元素不加顿号,那么新增一个元素时,必然要在最后补一个顿号,这就造成了两行代码变更,会让审查代码的人很困惑。

9

rust 复制代码
// 就地修复错误
fn main() {
    let age = Some(30);
    if let Some(age) = age { // 创建一个新的变量,该变量与之前的 `age` 变量同名
       assert_eq!(age, Some(30));
    } // 新的 `age` 变量在这里超出作用域
    
    match age {
        // `match` 也能实现变量遮蔽
        Some(age) =>  println!("age 是一个新的变量,它的值是 {}",age),
        _ => ()
    }
 }

答案:

rust 复制代码
fn main() {
    let age = Some(30);
    if let Some(age) = age { // 创建一个新的变量,该变量与之前的 `age` 变量同名
       assert_eq!(age, 30);
    } // 新的 `age` 变量在这里超出作用域
    
    match age {
        // `match` 也能实现变量遮蔽
        Some(age) =>  println!("age 是一个新的变量,它的值是 {}",age),
        _ => ()
    }
 }
相关推荐
阿华的代码王国3 分钟前
【JavaEE】多线程编程引入——认识Thread类
java·开发语言·数据结构·mysql·java-ee
繁依Fanyi9 分钟前
828 华为云征文|华为 Flexus 云服务器部署 RustDesk Server,打造自己的远程桌面服务器
运维·服务器·开发语言·人工智能·pytorch·华为·华为云
andrew_121911 分钟前
腾讯 IEG 游戏前沿技术 二面复盘
后端·sql·面试
Lill_bin21 分钟前
JVM内部结构解析
jvm·后端·spring cloud·微服务·云原生·ribbon
weixin_4866811425 分钟前
C++系列-STL容器中统计算法count, count_if
开发语言·c++·算法
基德爆肝c语言25 分钟前
C++入门
开发语言·c++
怀九日31 分钟前
C++(学习)2024.9.18
开发语言·c++·学习·面向对象·引用·
一道秘制的小菜32 分钟前
C++第七节课 运算符重载
服务器·开发语言·c++·学习·算法
易辰君1 小时前
Python编程 - 协程
开发语言·python
evanYang_1 小时前
Spring Boot配置文件敏感信息加密
spring boot·后端·oracle