IT评测·应用市场-qidao123.com技术社区

标题: 【rCore OS 开源利用系统】Rust 枚举与模式匹配 [打印本页]

作者: 南飓风    时间: 2024-10-9 17:44
标题: 【rCore OS 开源利用系统】Rust 枚举与模式匹配
【rCore OS 开源利用系统】Rust 枚举与模式匹配

前言

现在固然人在大阪,但是还是把笔记本是从来都带身上的。
每天玩了之后,晚上还可以学一学rust写写代码。

言归正传,本章节涉及到的知识点有:

知识点

训练题

option1

标题

  1. // options1.rs
  2. //
  3. // Execute `rustlings hint options1` or use the `hint` watch subcommand for a
  4. // hint.
  5. // I AM NOT DONE
  6. // This function returns how much icecream there is left in the fridge.
  7. // If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
  8. // all, so there'll be no more left :(
  9. fn maybe_icecream(time_of_day: u16) -> Option<u16> {
  10.     // We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a
  11.     // value of 0 The Option output should gracefully handle cases where
  12.     // time_of_day > 23.
  13.     // TODO: Complete the function body - remember to return an Option!
  14.     ???
  15. }
  16. #[cfg(test)]
  17. mod tests {
  18.     use super::*;
  19.     #[test]
  20.     fn check_icecream() {
  21.         assert_eq!(maybe_icecream(9), Some(5));
  22.         assert_eq!(maybe_icecream(10), Some(5));
  23.         assert_eq!(maybe_icecream(23), Some(0));
  24.         assert_eq!(maybe_icecream(22), Some(0));
  25.         assert_eq!(maybe_icecream(25), None);
  26.     }
  27.     #[test]
  28.     fn raw_value() {
  29.         // TODO: Fix this test. How do you get at the value contained in the
  30.         // Option?
  31.         let icecreams = maybe_icecream(12);
  32.         assert_eq!(icecreams, 5);
  33.     }
  34. }
复制代码
题解

没有特别难的点,主要是搞懂标题的意思:
   22 点之前,冰箱里都有 5 个冰淇淋




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4