IT评测·应用市场-qidao123.com技术社区
标题:
【rCore OS 开源利用系统】Rust 枚举与模式匹配
[打印本页]
作者:
南飓风
时间:
2024-10-9 17:44
标题:
【rCore OS 开源利用系统】Rust 枚举与模式匹配
【rCore OS 开源利用系统】Rust 枚举与模式匹配
前言
现在固然人在大阪,但是还是把笔记本是从来都带身上的。
每天玩了之后,晚上还可以学一学rust写写代码。
言归正传,本章节涉及到的知识点有:
枚举的应用:这里不再是基本语法,而是要学会用。
模式匹配实战:利用 match来处理题目。
语法糖:利用where let和if let来处理简化代码。
引用与借用:复习一下。
知识点
训练题
option1
标题
// options1.rs
//
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a
// hint.
// I AM NOT DONE
// This function returns how much icecream there is left in the fridge.
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
// all, so there'll be no more left :(
fn maybe_icecream(time_of_day: u16) -> Option<u16> {
// We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a
// value of 0 The Option output should gracefully handle cases where
// time_of_day > 23.
// TODO: Complete the function body - remember to return an Option!
???
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_icecream() {
assert_eq!(maybe_icecream(9), Some(5));
assert_eq!(maybe_icecream(10), Some(5));
assert_eq!(maybe_icecream(23), Some(0));
assert_eq!(maybe_icecream(22), Some(0));
assert_eq!(maybe_icecream(25), None);
}
#[test]
fn raw_value() {
// TODO: Fix this test. How do you get at the value contained in the
// Option?
let icecreams = maybe_icecream(12);
assert_eq!(icecreams, 5);
}
}
复制代码
题解
没有特别难的点,主要是搞懂标题的意思:
22 点之前,冰箱里都有 5 个冰淇淋
欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/)
Powered by Discuz! X3.4