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

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.

主站蜘蛛池模板: 吴江市| 郯城县| 满洲里市| 友谊县| 巴里| 海晏县| 平昌县| 克东县| 万盛区| 白山市| 万宁市| 阜宁县| 和田市| 石楼县| 濮阳市| 惠来县| 六盘水市| 和林格尔县| 紫金县| 拉萨市| 应城市| 靖远县| 汽车| 通州市| 彰武县| 临西县| 林西县| 甘泉县| 红河县| 岑巩县| 贺州市| 依兰县| 台湾省| 增城市| 博爱县| 成安县| 富阳市| 贵定县| 三门县| 临潭县| 西乌珠穆沁旗|