官术网_书友最值得收藏!

Enumerations

While a structure allows us to get multiple values under the same variable, enumerations allow us to choose one value from different types of values.

For example, let's write a type representing an expression:

enum Expr {
    Null,
    Add(i32, i32),
    Sub(i32, i32),
    Mul(i32, i32),
    Div { pidend: i32, pisor: i32 },
    Val(i32),
}

let quotient = Expr::Div { pidend: 10, pisor: 2 };
let sum = Expr::Add(40, 2);

The Null variant does not have a value associated with it, Val has one associated value, and Add has two. Div also has two associated values, but they are named, similar to how we define a structure.

主站蜘蛛池模板: 长武县| 安龙县| 安阳县| 桦南县| 河南省| 上饶市| 马尔康县| 合作市| 榕江县| 奉化市| 垣曲县| 全州县| 昌黎县| 潮州市| 合肥市| 兰考县| 铁岭市| 沙雅县| 丰顺县| 苍溪县| 黔南| 牙克石市| 大丰市| 会东县| 泽普县| 南木林县| 麻阳| 曲沃县| 富裕县| 大石桥市| 鸡泽县| 泗洪县| 满城县| 武安市| 富顺县| 万源市| 高邑县| 浪卡子县| 大方县| 宜宾县| 新巴尔虎左旗|