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

devtools/2024/10/8 13:21:32/

OS_Rust__Enums_1">【rCore OS 开源操作系统】Rust 练习题题解: Enums

摘要

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

正文

enums1

题目
rust">// enums1.rs
//
// No hints this time! ;)// I AM NOT DONE#[derive(Debug)]
enum Message {// TODO: define a few types of messages as used below
}fn main() {println!("{:?}", Message::Quit);println!("{:?}", Message::Echo);println!("{:?}", Message::Move);println!("{:?}", Message::ChangeColor);
}
题解

目测就是基本的枚举值语法。
甚至简单到题目中出现了 No hints this time! ;),不会做那就有点汗颜了。

参考资料:https://doc.rust-lang.org/stable/book/ch06-01-defining-an-enum.html

rust">// enums1.rs
//
// No hints this time! ;)#[derive(Debug)]
enum Message {// TODO: define a few types of messages as used below
}fn main() {println!("{:?}", Message::Quit);println!("{:?}", Message::Echo);println!("{:?}", Message::Move);println!("{:?}", Message::ChangeColor);
}

enums2

这里的核心知识点是,枚举与数据类型关联。

题目
rust">// enums2.rs
//
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
// hint.// I AM NOT DONE#[derive(Debug)]
enum Message {// TODO: define the different variants used below
}impl Message {fn call(&self) {println!("{:?}", self);}
}fn main() {let messages = [Message::Move { x: 10, y: 30 },Message::Echo(String::from("hello world")),Message::ChangeColor(200, 255, 255),Message::Quit,];for message in &messages {message.call();}
}
题解

题解与上面一样。

rust">// enums2.rs
//
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
// hint.#[derive(Debug)]
enum Message {// TODO: define the different variants used belowMove { x: i32, y: i32 },Echo(String),ChangeColor(i32, i32, i32),Quit,
}impl Message {fn call(&self) {println!("{:?}", self);}
}fn main() {let messages = [Message::Move { x: 10, y: 30 },Message::Echo(String::from("hello world")),Message::ChangeColor(200, 255, 255),Message::Quit,];for message in &messages {message.call();}
}

enums3

题目
rust">// enums3.rs
//
// Address all the TODOs to make the tests pass!
//
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
// hint.// I AM NOT DONEenum Message {// TODO: implement the message variant types based on their usage below
}struct Point {x: u8,y: u8,
}struct State {color: (u8, u8, u8),position: Point,quit: bool,message: String
}impl State {fn change_color(&mut self, color: (u8, u8, u8)) {self.color = color;}fn quit(&mut self) {self.quit = true;}fn echo(&mut self, s: String) { self.message = s }fn move_position(&mut self, p: Point) {self.position = p;}fn process(&mut self, message: Message) {// TODO: create a match expression to process the different message// variants// Remember: When passing a tuple as a function argument, you'll need// extra parentheses: fn function((t, u, p, l, e))}
}#[cfg(test)]
mod tests {use super::*;#[test]fn test_match_message_call() {let mut state = State {quit: false,position: Point { x: 0, y: 0 },color: (0, 0, 0),message: "hello world".to_string(),};state.process(Message::ChangeColor(255, 0, 255));state.process(Message::Echo(String::from("hello world")));state.process(Message::Move(Point { x: 10, y: 15 }));state.process(Message::Quit);assert_eq!(state.color, (255, 0, 255));assert_eq!(state.position.x, 10);assert_eq!(state.position.y, 15);assert_eq!(state.quit, true);assert_eq!(state.message, "hello world");}
}
题解

在要求会用枚举的基础上,结合了常常配合枚举一起使用的模式匹配

rust">// enums3.rs
//
// Address all the TODOs to make the tests pass!
//
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
// hint.enum Message {// TODO: implement the message variant types based on their usage belowChangeColor(u8, u8, u8),Echo(String),Move(Point),Quit,
}struct Point {x: u8,y: u8,
}struct State {color: (u8, u8, u8),position: Point,quit: bool,message: String,
}impl State {fn change_color(&mut self, color: (u8, u8, u8)) {self.color = color;}fn quit(&mut self) {self.quit = true;}fn echo(&mut self, s: String) {self.message = s}fn move_position(&mut self, p: Point) {self.position = p;}fn process(&mut self, message: Message) {// TODO: create a match expression to process the different message// variants// Remember: When passing a tuple as a function argument, you'll need// extra parentheses: fn function((t, u, p, l, e))match message {Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),Message::Echo(string) => self.echo(string),Message::Move(point) => self.move_position(point),Message::Quit => self.quit(),}}
}#[cfg(test)]
mod tests {use super::*;#[test]fn test_match_message_call() {let mut state = State {quit: false,position: Point { x: 0, y: 0 },color: (0, 0, 0),message: "hello world".to_string(),};state.process(Message::ChangeColor(255, 0, 255));state.process(Message::Echo(String::from("hello world")));state.process(Message::Move(Point { x: 10, y: 15 }));state.process(Message::Quit);assert_eq!(state.color, (255, 0, 255));assert_eq!(state.position.x, 10);assert_eq!(state.position.y, 15);assert_eq!(state.quit, true);assert_eq!(state.message, "hello world");}
}

http://www.ppmy.cn/devtools/121668.html

相关文章

rabbitMq-----消费者管理模块

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言消费者字段消费者内存管理类新增/删除消费者 总的消费者管理类 前言 只有订阅客户端订阅了一个队列消息后,他才是一个消费者,而消费者存…

SpringBoot教程(安装篇) | Docker Desktop的安装(Windows下的Docker环境)

SpringBoot教程(安装篇) | Docker Desktop的安装(Windows下的Docker环境) 前言如何安装Docker Desktop资源下载安装启动(重点)1. 检查 bcdedit的hypervisorlaunchtype是否为Auto2. 检查CPU是否开启虚拟化3.…

程序猿成长之路之设计模式篇——设计模式简介

无论是对于代码质量还是代码可维护性、可扩展性,使用合适的设计模式都能够起到促进提升的作用,此外在软考的软件工程师、系统架构师职称考试中,设计模式也是必考的一块内容,因此我打算开拓一个新的专栏简单介绍一下设计模式&#…

黑马JavaWeb开发跟学(九)MyBatis基础操作

黑马JavaWeb开发跟学九.MyBatis基础操作 1. Mybatis基础操作1.1 需求1.2 准备1.3 删除1.3.1 功能实现1.3.2 日志输入1.3.3 预编译SQL1.3.3.1 介绍1.3.3.2 SQL注入1.3.3.3 参数占位符 1.4 新增1.4.1 基本新增1.4.2 主键返回 1.5 更新1.6 查询1.6.1 根据ID查询1.6.2 数据封装1.6.…

【AI大模型-文心-思维树解读-总结的总结-最后】

提问:总结的总结,请根据我的 “”“什么是“”“思维树”“”模型框架 “”“,到最后”“” 好的,确认”"“仓颉精通之路”"“作为我们的”"“暗号”"" “”“,总结出我们对话的方法论 回答&…

Discord:报错:A fatal Javascript error occured(解决办法)

按 Windows 键 R 并输入 %appdata% 选择 discord 文件夹并将其删除。 再次按 Windows 键 R 并输入 %LocalAppData% 选择 discord 文件夹并再次将其删除。 附加: 如果还不行,就通过官网下载吧,这个问题通过epic下载可能会有

编码与解码

文章目录 编码与解码一、字节 & 字符二、编码 & 解码三、字符集 & 字符编码四、ASCII五、ISO-8859-1六、GB七、Unicode1、概述2、发展3、UTF-8 编码4、UTF-16 编码 八、Base64 编码1、概述2、原理3、代码示例 九、十六进制编码 编码与解码 一、字节 & 字符 字…

雷池 WAF 如何配置才能正确获取到源 IP

经常有大哥反馈说雷池攻击日志里显示的 IP 有问题。 这里我来讲一下为什么一些情况下雷池显示的攻击 IP 会有问题。 问题说明 默认情况下,雷池会通过 HTTP 连接的 Socket 套接字读取客户端 IP。在雷池作为最外层网管设备的时候这没有问题,雷池获取到的…