add useful links for 'Rust Error Handling' (#5936)
parent
5dff9b20e1
commit
09e345f48b
3 changed files with 16 additions and 3 deletions
@ -1,3 +1,8 @@ |
||||
# Option & Result Enumerations |
||||
|
||||
`Option` is an enumeration, also known as an `enum`, in Rust with two variants: `Some(T)` and `None`. It is a flexible and safe alternative to using `null` values or exceptions for indicating that a value might not be present. If a function may not return a value for all possible input, it should return an `Option` value. `Some(T)` variant contains the valid value of type `T` and `None` variant indicates the absence of a value. You can perform various operations on `Option` values, such as method chaining and pattern matching, to effectively handle both states (value present or absent). The `Option` enum encourages you to consciously deal with the possibility of missing values and helps prevent unforeseen runtime errors. |
||||
`Option` is an enumeration, also known as an `enum`, in Rust with two variants: `Some(T)` and `None`. It is a flexible and safe alternative to using `null` values or exceptions for indicating that a value might not be present. If a function may not return a value for all possible input, it should return an `Option` value. `Some(T)` variant contains the valid value of type `T` and `None` variant indicates the absence of a value. You can perform various operations on `Option` values, such as method chaining and pattern matching, to effectively handle both states (value present or absent). The `Option` enum encourages you to consciously deal with the possibility of missing values and helps prevent unforeseen runtime errors. |
||||
|
||||
Visit the following resources to learn more: |
||||
|
||||
- [@article@Rust by Example: Option & unwrap](https://doc.rust-lang.org/rust-by-example/error/option_unwrap.html) |
||||
- [@article@Rust by Example: Result](https://doc.rust-lang.org/rust-by-example/error/result.html) |
||||
|
@ -1,3 +1,7 @@ |
||||
# Propagating Errors and `?` Operator |
||||
|
||||
Propagating errors in Rust is about passing the error information from the function that failed to the function that called it. Using the `?` operator is one way to achieve this. This operator can only be used in functions that return `Result` or `Option` or another type that implements `std::ops::Try`. If the value of the `Result` is `Ok`, the value inside the `Ok` will get returned. If the value is `Err`, the `Err` will be returned from the whole function. Consequently, the error gets propagated to the calling function. |
||||
Propagating errors in Rust is about passing the error information from the function that failed to the function that called it. Using the `?` operator is one way to achieve this. This operator can only be used in functions that return `Result` or `Option` or another type that implements `std::ops::Try`. If the value of the `Result` is `Ok`, the value inside the `Ok` will get returned. If the value is `Err`, the `Err` will be returned from the whole function. Consequently, the error gets propagated to the calling function. |
||||
|
||||
Visit the following resources to learn more: |
||||
|
||||
- [@article@Rust Book: Recoverable Errors with Result](https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html) |
||||
|
@ -1,3 +1,7 @@ |
||||
# Custom Error Types and Traits |
||||
|
||||
In Rust, you can define your own types of errors using the `enum` construct. This gives you the ability to specify different types of errors that your code can encounter, and attach additional information to them. To make your custom error type compatible with the rest of Rust's error handling machinery, you need to implement two traits: `std::fmt::Debug` and `std::fmt::Display`. There is also a third trait, `std::error::Error`, which can provide backtraces and the ability to have chained errors or causes. Furthermore, the `thiserror` library provides a convenient way to define custom error types with a simple annotation. |
||||
In Rust, you can define your own types of errors using the `enum` construct. This gives you the ability to specify different types of errors that your code can encounter, and attach additional information to them. To make your custom error type compatible with the rest of Rust's error handling machinery, you need to implement two traits: `std::fmt::Debug` and `std::fmt::Display`. There is also a third trait, `std::error::Error`, which can provide backtraces and the ability to have chained errors or causes. Furthermore, the `thiserror` library provides a convenient way to define custom error types with a simple annotation. |
||||
|
||||
Visit the following resources to learn more: |
||||
|
||||
- [@article@Rust by Example: Defining an error type](https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/define_error_type.html) |
||||
|
Loading…
Reference in new issue