Failing With Style
It may seem weird that this None
propagation thing is considered so important in Rust that it gets its own special ?
thingy. But here’s the next piece in the puzzle: ?
works for more than just Option
. A very common thing to do in real world programmers is to deal with errors. Let’s get a nice example to make sense of that.
Let’s say I’m running the ticket booth for a roller coaster. Families come up, and I need to figure out how much it will cost them to ride. The rule is:
-
18 years old and under cost 5
-
19 years old to 65 years old cost 8
-
66 years old and up cost 7
However, there’s one more thing, no one under 10, or shorter than 80 centimeters, can ride. Let’s first write a data type and function that figures out the price for a single person: