【rCore OS 开源操作系统】Rust 训练题题解: Enums

农民  金牌会员 | 2024-10-7 04:55:25 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 885|帖子 885|积分 2655

【rCore OS 开源操作系统】Rust 训练题题解: Enums

择要

   rCore OS 开源操作系统训练营学习中的代码训练部分。
在此记录下自己学习过程中的产物,以便于日后更有“收获感”。
后续还会继续完成其他章节的训练题题解。
  正文

enums1

题目

  1. // enums1.rs
  2. //
  3. // No hints this time! ;)
  4. // I AM NOT DONE
  5. #[derive(Debug)]
  6. enum Message {
  7.     // TODO: define a few types of messages as used below
  8. }
  9. fn main() {
  10.     println!("{:?}", Message::Quit);
  11.     println!("{:?}", Message::Echo);
  12.     println!("{:?}", Message::Move);
  13.     println!("{:?}", Message::ChangeColor);
  14. }
复制代码
题解

目测就是基本的枚举值语法。
甚至简朴到题目中出现了 No hints this time! ;),不会做那就有点汗颜了。
   参考资料:https://doc.rust-lang.org/stable/book/ch06-01-defining-an-enum.html
  1. // enums1.rs
  2. //
  3. // No hints this time! ;)
  4. #[derive(Debug)]
  5. enum Message {
  6.     // TODO: define a few types of messages as used below
  7. }
  8. fn main() {
  9.     println!("{:?}", Message::Quit);
  10.     println!("{:?}", Message::Echo);
  11.     println!("{:?}", Message::Move);
  12.     println!("{:?}", Message::ChangeColor);
  13. }
复制代码
enums2

这里的焦点知识点是,枚举与数据类型关联。
题目

  1. // enums2.rs
  2. //
  3. // Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
  4. // hint.
  5. // I AM NOT DONE
  6. #[derive(Debug)]
  7. enum Message {
  8.     // TODO: define the different variants used below
  9. }
  10. impl Message {
  11.     fn call(&self) {
  12.         println!("{:?}", self);
  13.     }
  14. }
  15. fn main() {
  16.     let messages = [
  17.         Message::Move { x: 10, y: 30 },
  18.         Message::Echo(String::from("hello world")),
  19.         Message::ChangeColor(200, 255, 255),
  20.         Message::Quit,
  21.     ];
  22.     for message in &messages {
  23.         message.call();
  24.     }
  25. }
复制代码
题解

题解与上面一样。
  1. // enums2.rs
  2. //
  3. // Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
  4. // hint.
  5. #[derive(Debug)]
  6. enum Message {
  7.     // TODO: define the different variants used below
  8.     Move { x: i32, y: i32 },
  9.     Echo(String),
  10.     ChangeColor(i32, i32, i32),
  11.     Quit,
  12. }
  13. impl Message {
  14.     fn call(&self) {
  15.         println!("{:?}", self);
  16.     }
  17. }
  18. fn main() {
  19.     let messages = [
  20.         Message::Move { x: 10, y: 30 },
  21.         Message::Echo(String::from("hello world")),
  22.         Message::ChangeColor(200, 255, 255),
  23.         Message::Quit,
  24.     ];
  25.     for message in &messages {
  26.         message.call();
  27.     }
  28. }
复制代码
enums3

题目

  1. // enums3.rs
  2. //
  3. // Address all the TODOs to make the tests pass!
  4. //
  5. // Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
  6. // hint.
  7. // I AM NOT DONE
  8. enum Message {
  9.     // TODO: implement the message variant types based on their usage below
  10. }
  11. struct Point {
  12.     x: u8,
  13.     y: u8,
  14. }
  15. struct State {
  16.     color: (u8, u8, u8),
  17.     position: Point,
  18.     quit: bool,
  19.     message: String
  20. }
  21. impl State {
  22.     fn change_color(&mut self, color: (u8, u8, u8)) {
  23.         self.color = color;
  24.     }
  25.     fn quit(&mut self) {
  26.         self.quit = true;
  27.     }
  28.     fn echo(&mut self, s: String) { self.message = s }
  29.     fn move_position(&mut self, p: Point) {
  30.         self.position = p;
  31.     }
  32.     fn process(&mut self, message: Message) {
  33.         // TODO: create a match expression to process the different message
  34.         // variants
  35.         // Remember: When passing a tuple as a function argument, you'll need
  36.         // extra parentheses: fn function((t, u, p, l, e))
  37.     }
  38. }
  39. #[cfg(test)]
  40. mod tests {
  41.     use super::*;
  42.     #[test]
  43.     fn test_match_message_call() {
  44.         let mut state = State {
  45.             quit: false,
  46.             position: Point { x: 0, y: 0 },
  47.             color: (0, 0, 0),
  48.             message: "hello world".to_string(),
  49.         };
  50.         state.process(Message::ChangeColor(255, 0, 255));
  51.         state.process(Message::Echo(String::from("hello world")));
  52.         state.process(Message::Move(Point { x: 10, y: 15 }));
  53.         state.process(Message::Quit);
  54.         assert_eq!(state.color, (255, 0, 255));
  55.         assert_eq!(state.position.x, 10);
  56.         assert_eq!(state.position.y, 15);
  57.         assert_eq!(state.quit, true);
  58.         assert_eq!(state.message, "hello world");
  59.     }
  60. }
复制代码
题解

在要求会用枚举的基础上,联合了常常共同枚举一起使用的模式匹配
  1. // enums3.rs
  2. //
  3. // Address all the TODOs to make the tests pass!
  4. //
  5. // Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
  6. // hint.
  7. enum Message {
  8.     // TODO: implement the message variant types based on their usage below
  9.     ChangeColor(u8, u8, u8),
  10.     Echo(String),
  11.     Move(Point),
  12.     Quit,
  13. }
  14. struct Point {
  15.     x: u8,
  16.     y: u8,
  17. }
  18. struct State {
  19.     color: (u8, u8, u8),
  20.     position: Point,
  21.     quit: bool,
  22.     message: String,
  23. }
  24. impl State {
  25.     fn change_color(&mut self, color: (u8, u8, u8)) {
  26.         self.color = color;
  27.     }
  28.     fn quit(&mut self) {
  29.         self.quit = true;
  30.     }
  31.     fn echo(&mut self, s: String) {
  32.         self.message = s
  33.     }
  34.     fn move_position(&mut self, p: Point) {
  35.         self.position = p;
  36.     }
  37.     fn process(&mut self, message: Message) {
  38.         // TODO: create a match expression to process the different message
  39.         // variants
  40.         // Remember: When passing a tuple as a function argument, you'll need
  41.         // extra parentheses: fn function((t, u, p, l, e))
  42.         match message {
  43.             Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
  44.             Message::Echo(string) => self.echo(string),
  45.             Message::Move(point) => self.move_position(point),
  46.             Message::Quit => self.quit(),
  47.         }
  48.     }
  49. }
  50. #[cfg(test)]
  51. mod tests {
  52.     use super::*;
  53.     #[test]
  54.     fn test_match_message_call() {
  55.         let mut state = State {
  56.             quit: false,
  57.             position: Point { x: 0, y: 0 },
  58.             color: (0, 0, 0),
  59.             message: "hello world".to_string(),
  60.         };
  61.         state.process(Message::ChangeColor(255, 0, 255));
  62.         state.process(Message::Echo(String::from("hello world")));
  63.         state.process(Message::Move(Point { x: 10, y: 15 }));
  64.         state.process(Message::Quit);
  65.         assert_eq!(state.color, (255, 0, 255));
  66.         assert_eq!(state.position.x, 10);
  67.         assert_eq!(state.position.y, 15);
  68.         assert_eq!(state.quit, true);
  69.         assert_eq!(state.message, "hello world");
  70.     }
  71. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

农民

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表