当前位置: 首页 > news >正文

自学编程做点网站赚钱建设集团有限公司是什么意思

自学编程做点网站赚钱,建设集团有限公司是什么意思,梅州做网站wlwl,seo咨询岳阳【rCore OS 开源操作系统】Rust 练习题题解: Enums 摘要 rCore OS 开源操作系统训练营学习中的代码练习部分。 在此记录下自己学习过程中的产物,以便于日后更有“收获感”。 后续还会继续完成其他章节的练习题题解。 正文 enums1 题目 // enums1.rs // // No hi…

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

摘要

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

正文

enums1

题目
// 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

// 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

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

题目
// 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();}
}
题解

题解与上面一样。

// 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

题目
// 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");}
}
题解

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

// 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.yayakq.cn/news/861919/

相关文章:

  • 制作企业网站一般多少钱电商网站设计主题
  • 做淘客网站怎么全网维护
  • 做网站的人会留下啥漏洞吗国外网站怎么做引流
  • 校园网站建设用什么软件写平度推广网站建设
  • 车险保险网站小程序设计需要多少钱
  • 做苗木行业网站赚钱南宁网络优化seo费用
  • 网站建设怎么做账务处理广安公司网站建设
  • 100个免费推广网站下载优秀网站seo报价
  • 深圳专业网站设计哪家好西安市工程建设信息网
  • 实用网站的设计与实现斗鱼网站的实时视频是怎么做的
  • 聊城那里做网站弹簧机东莞网站建设
  • 深圳设计网站建设公司寓意前程似锦的工程公司名字
  • 医院门户网站设计登录免费注册网址
  • 网站设计网站制作c mvc网站开发
  • 网站建设与网页设计案例教程军事新闻最新消息11
  • 桂平市住房和城乡建设局门户网站广州安全教育平台官网
  • 福州网站建设找嘉艺网络搭建一个网站的步骤
  • 成都网站建设方案服务零基础自学python
  • flash网站设计作品wordpress更新服务ping
  • 网站建设维护推广合同为什么企业要上市
  • 龙华做棋牌网站建设哪家好下沙经济开发区建设局网站
  • 志丹网站建设上海网站建设 上海网站制作
  • 做英文网站哪家好南京网站制作公司电话
  • 任丘网站开发建设怎么选汉中市建设工程招投标信息网官网
  • 网站建设周期表国内永久免费crm系统软件高清完整版
  • dw网站模板免费下载网站建设要符合哪些标准
  • 西安淘宝网站建设公司企业的网站建设费账务处理
  • 打广告型的营销网站制作短视频的软件
  • 黄冈网站推广优化找哪家建设免费网站制作
  • 大兴安岭地网站seo信息查询