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

  • 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.

主站蜘蛛池模板: 大宁县| 崇仁县| 余干县| 江油市| 鄂托克前旗| 无极县| 北流市| 崇文区| 南昌市| 固原市| 石楼县| 菏泽市| 子洲县| 翼城县| 崇仁县| 黄石市| 防城港市| 巴中市| 华蓥市| 伊宁县| 乌什县| 乌拉特后旗| 宜城市| 罗山县| 九江县| 望奎县| 太谷县| 屏边| 新疆| 北宁市| 罗源县| 沅江市| 荃湾区| 宁南县| 康定县| 龙岩市| 环江| 陆丰市| 赣榆县| 武山县| 贵定县|