有没有更简单的方法使枚举常量可见? [英] Is there an easier way to make enum constants visible?

查看:57
本文介绍了有没有更简单的方法使枚举常量可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己正在写这样的东西:

I find myself writing stuff like this:

pub enum Player {BLACK, WHITE,}

const BLACK: Player = Player::BLACK;
const WHITE: Player = Player::WHITE;

当然,原因是要避免在匹配表达式和常量的其他用法中产生干扰。

The reason, of course, being to avoid noise in match expressions and other uses of the constants.

是否有更简单的方法来实现此目的?

Is there an easier way to achieve this?

推荐答案

是的,只需使用 use 关键字。

Yes, just import the enum variants with the use keyword.

pub enum Player {
    Black,
    White,
}

use Player::*;

这篇关于有没有更简单的方法使枚举常量可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆