enums - 是否可以将整数转换为枚举并进行详尽匹配?
问题描述
我的程序以数字的形式接收用户的命令:let command: int
. 该命令应该是枚举值之一:
enum Command {
Launch,
Stop,
}
如何将command
整数转换为Command
枚举?将类型从整数转换为枚举后,如何彻底匹配该枚举并处理command
' 值超出Command
' 值范围的可能性?看代码:
match command { // command has `enum Command` type here already
Launch => { /* ... */ }
Stop => { /* ... */ }
_ => { /* ... */ } // command is user input, so it can have any value, but Rust will yell "unreachable pattern" here
}
解决方案
推荐阅读
- javascript - Iterate through nested objects
- java - Copied DocumentFile has different siize and hash to original
- javascript - 如何从 JavaScript 中的函数调用中获取值以显示在 PHP 页面中?
- r - Automatic Highlighting Outliers in ggplots
- r - 是否有一个 R 函数来获取变量在几年内多次出现的最大值?
- mysql - MySql 意外查询结果
- ios - 获取模态呈现的视图控制器高度 ios 13
- haskell - 在 GHC 中 enum 和 Int 之间转换的最快方法是什么?
- java - Change from LinkedList library to custom class
- javascript - Two callbacks as argument for Array.sort(), rather than Array.sort().sort()