Is there an easier way to make enum constants visible?

徘徊边缘 提交于 2021-01-20 06:08:21

问题


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?


回答1:


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

pub enum Player {
    Black,
    White,
}

use Player::*;


来源:https://stackoverflow.com/questions/64266286/is-there-an-easier-way-to-make-enum-constants-visible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!