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

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.

主站蜘蛛池模板: 揭阳市| 健康| 正安县| 新安县| 深泽县| 呼伦贝尔市| 岗巴县| 泽州县| 鹤峰县| 阜新| 枞阳县| 商南县| 盱眙县| 漾濞| 水城县| 伊春市| 南川市| 商都县| 射洪县| 阿巴嘎旗| 潜江市| 绥阳县| 饶平县| 罗田县| 文登市| 山丹县| 城市| 通河县| 通渭县| 鄂托克旗| 六盘水市| 肥东县| 东港市| 高安市| 高青县| 阳曲县| 肃宁县| 九寨沟县| 吉木乃县| 梁平县| 文安县|