Rust Programming Cookbook
上QQ阅读APP看书,第一时间看更新

Creating meaningful numbers with enums

Enums, short for enumerations, are well-known programming constructs that many languages feature. These special cases of types allow a number to be mapped to a name. This can be used to tie constants together under a single name and lets us declare values as variants. For example, we could have pi, as well as Euler's number, as variants of an enum, MathConstants. Rust is no different, but it can go a lot further. Instead of simply relying on naming numbers, Rust allows enums the same flexibility as other Rust types have. Let's see what this means in practice.