首页 > 解决方案 > 是否可以将整数转换为枚举并进行详尽匹配?

问题描述

我的程序以数字的形式接收用户的命令: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
}

标签: enumsrustpattern-matching

解决方案


推荐阅读