(done) 速通 rustlings(19) Option

Option 关键字

在 Rust 里,当某个操作可能会有值也可能没有值时,我们不会用 null,而是使用 Option 枚举:

rust 复制代码
enum Option<T> {
    Some(T),  // 有值,里面包含一个 T 类型的数据
    None,     // 没有值
}
  • 它强制你在使用前处理"无值"的情况,避免空指针错误。
  • 常见的方法包括 unwrap()(直接取值,若是 None 会 panic)、match、if let、map、and_then 等。

具体代码例子如下:

rust 复制代码
// This function returns how much icecream there is left in the fridge.
// If it's before 22:00 (24-hour system), then 5 scoops are left. At 22:00,
// someone eats it all, so no icecream is left (value 0). Return `None` if
// `hour_of_day` is higher than 23.
fn maybe_icecream(hour_of_day: u16) -> Option<u16> {
    // hour_of_day is in 0..=23; if it's >23 we return None
    // before 22:00 we have 5 scoops, at 22 or 23 we have 0 scoops.
    // if hour_of_day > 23 {
    //     None
    // } else if hour_of_day < 22 {
    //     Some(5)
    // } else {
    //     // 22 or 23
    //     Some(0)
    // }

    match hour_of_day {
        0..=21 => Some(5),
        22..=23 => Some(0),
        _ => None,
    }
}

fn main() {
    // You can optionally experiment here.
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn raw_value() {
        // extract the inner value using unwrap since we know it's Some
        let icecreams = maybe_icecream(12).unwrap();

        assert_eq!(icecreams, 5); // Don't change this line.
    }

    #[test]
    fn check_icecream() {
        assert_eq!(maybe_icecream(0), Some(5));
        assert_eq!(maybe_icecream(9), Some(5));
        assert_eq!(maybe_icecream(18), Some(5));
        assert_eq!(maybe_icecream(22), Some(0));
        assert_eq!(maybe_icecream(23), Some(0));
        assert_eq!(maybe_icecream(24), None);
        assert_eq!(maybe_icecream(25), None);
    }
}

if-let 和 while-let

看代码,理解 if-let 和 while-let 的作用

rust 复制代码
fn main() {
    // You can optionally experiment here.
}

#[cfg(test)]
mod tests {
    #[test]
    fn simple_option() {
        let target = "rustlings";
        let optional_target = Some(target);

        // TODO: Make this an if-let statement whose value is `Some`.
        if let Some(word) = optional_target {
            assert_eq!(word, target);
        }
    }

    #[test]
    fn layered_option() {
        let range = 10;
        let mut optional_integers: Vec<Option<i8>> = vec![None];

        for i in 1..=range {
            optional_integers.push(Some(i));
        }

        let mut cursor = range;

        // TODO: Make this a while-let statement. Remember that `Vec::pop()`
        // adds another layer of `Option`. You can do nested pattern matching
        // in if-let and while-let statements.
        // Vec::pop() 会多加一层 Option,所以需要匹配 Some(Some(integer))
        while let Some(Some(integer)) = optional_integers.pop() {
            assert_eq!(integer, cursor);
            cursor -= 1;
        }

        assert_eq!(cursor, 0);
    }
}

match关键字 与 Option关键字 配合

match 中不需要使用 let 去声明 p,模式匹配中隐含了 let 声明关键字

rust 复制代码
#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let optional_point = Some(Point { x: 100, y: 200 });

    // Solution 1: Matching over the `Option` (not `&Option`) but without moving
    // out of the `Some` variant.
    match optional_point {
        Some(ref p) => println!("Co-ordinates are {},{}", p.x, p.y),
        //   ^^^ added
        _ => panic!("No match!"),
    }

    // Solution 2: Matching over a reference (`&Option`) by added `&` before
    // `optional_point`.
    match &optional_point {
        Some(p) => println!("Co-ordinates are {},{}", p.x, p.y),
        _ => panic!("No match!"),
    }

    println!("{optional_point:?}");
}

相关推荐
小灰灰搞电子6 小时前
Rust+Slint 实现蜂巢菜单源码分享
开发语言·rust·蜂巢菜单.
mldong15 小时前
流程实例如何沿着箭头走到结束:Rust 工作流引擎的执行内核
架构·rust
小灰灰搞电子1 天前
Rust+Slint 实现动态轮播图源码分享,支持动态删除、添加
开发语言·rust·slint·动态轮播图
今天AI了吗1 天前
大模型技术全景(一):AI、机器学习、深度学习,三者的关系到底是什么?一文搞懂
数据库·人工智能·python·sql·深度学习·机器学习·rust
Naylor1 天前
切片:指向一段数据,而不是全部
rust
阿翰1 天前
MiniPdf Office 转 PDF 支持 Rust 原生实现:3 MB 单文件部署、低内存
rust·minipdf
小灰灰搞电子1 天前
Rust+Slint 实现圆形变色按钮源码分享
rust·slint·变色按钮
flyxl1 天前
DataZen 架构设计(一):一个现代桌面数据库工具是如何构建的
rust
k4m7v2pz1 天前
用 rust-verb-shell 重写进程管理:从 game.sh 到 .rvs 的迁移实录
开发语言·后端·rust
mldong2 天前
同一份 15 个流程 JSON,第六种语言也跑通了:工作流引擎 Rust 移植实录
架构·rust