Using Option
In programming, a routine might produce a correct result or encounter a problem. One classical example is division by zero. Dividing something by zero is mathematically undefined. If the application has a routine to divide something, and the routine encounters zero as input, the application cannot return any number. We want the application to return another type instead of a number. We need a type that can hold multiple variants of data.
In Rust, we can define an enum
type, a type that can be different variants of data. An enum
type might be as follows:
enum Shapes {
None,
Point(i8),
Line(i8, i8),
Rectangle {
top: (i8, i8),
length: u8,
height: u8,
},
}
Point
and Line
are said...