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

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.

主站蜘蛛池模板: 中西区| 广州市| 和平县| 浦东新区| 平阴县| 绍兴市| 舒兰市| 昔阳县| 镇安县| 林周县| 景德镇市| 化德县| 江达县| 开封市| 嘉义市| 永城市| 虹口区| 延边| 龙里县| 莱西市| 景谷| 益阳市| 武乡县| 石首市| 阿坝| 巴马| 泰宁县| 商水县| 贺州市| 万源市| 汽车| 台安县| 竹山县| 乐清市| 远安县| 阳谷县| 光山县| 玉山县| 宜章县| 高安市| 齐河县|