How to Customize your Own Error Type in Rust
Suppose you are writing a lexical analyzer(Lexer
), and you want return some customized errors while the lexer scan tokens,
and there are 3 possibel error will happens:
- Not Supported Tokens.
- Invalid Strings.
- Invalid Nums.
SO you can define the errors with a rust enum
:
pub enum LexerError {
InvalidToken(char),
InvalidString(String),
InvalidNum(String),
}