Preventing bugs with defensive coding
You don’t need to fix bugs that never happen. Preventative medicine is good software engineering that will save you time in the long run.
Using Option and Result instead of panic!
In many other languages, exception handling is performed through try…catch
blocks. Rust does not automatically provide this functionality, instead it encourages the programmer to explicitly localize all error handling.
In many Rust contexts, if you don’t want to deal with error handling, you always have the option to use panic!
. This will immediately end the program and provide a short error message. Don't do this. Panicking is usually just a way of avoiding the responsibility of handling errors.
Instead, use either the Option
or Result
types to communicate error or exceptional conditions. Option
indicates that no value is available. The None
value of Option
should indicate that there is no value but that everything is okay and expected.
The Result
type is used to communicate whether...