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

  • Rust Programming By Example
  • Guillaume Gomez Antoni Boucher
  • 109字
  • 2021-07-02 19:12:59

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.

主站蜘蛛池模板: 高密市| 涪陵区| 林口县| 永嘉县| 车险| 汤阴县| 南和县| 咸宁市| 肥城市| 巴塘县| 德令哈市| 澄江县| 五原县| 庆安县| 怀安县| 开阳县| 灵山县| 浑源县| 布拖县| 三门县| 安平县| 芜湖县| 蕲春县| 自贡市| 米泉市| 独山县| 临高县| 新乡市| 西贡区| 莆田市| 防城港市| 高邑县| 丹阳市| 甘孜县| 岳阳市| 清新县| 尼勒克县| 收藏| 凌源市| 滕州市| 舒城县|