mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-06-15 04:54:17 -06:00
started ch9.2
This commit is contained in:
parent
1e81fbbf7b
commit
a6eeda7f49
@ -132,3 +132,46 @@ panic = 'abort'
|
||||
```
|
||||
|
||||
## Recoverable Errors
|
||||
|
||||
You can use the enum `Result` to handle most errors becasue they are not serious enough to warrant a panic
|
||||
|
||||
One example of this of a non serious error is opening a file and that operation fails becasue that file doesnt exist, you may want to create the file instead of terminating the process
|
||||
|
||||
the enum `Result` is defined as
|
||||
Which has two variants `Ok` and `Err`
|
||||
|
||||
```rust
|
||||
enum Result<T, E> {
|
||||
Ok(T),
|
||||
Err(E),
|
||||
}
|
||||
```
|
||||
|
||||
`T` and `E` are generic type parameters
|
||||
|
||||
`T` reperesents the tpye of value that will be returned in a success case within th `Ok` variant
|
||||
`E` reperesents the tpye of the error that will be returned in a failure case within the `Err` variant
|
||||
|
||||
Because `Result` has these generic type parameters we can use the `Result` type and the functions defined on it in many different situations where the success value and error value we want to reutrn may differ
|
||||
|
||||
Lets use a function that retunrs a `Result` value because the function could fail
|
||||
|
||||
```rust
|
||||
use std::fs::File;
|
||||
|
||||
fn main () {
|
||||
let greeting_file_result = File::open("hello.txt");
|
||||
}
|
||||
```
|
||||
|
||||
The return type of `File::open` is a `Result<T, E>`
|
||||
The generic parameter `T` has been filled in by the implementation of `File::open` with the type of the success value is a file handle (`std::fs::File`)
|
||||
The `E` parameter is used in the error value which is `std::io::Error`
|
||||
|
||||
This return type indicates that call may succeed and reutnr a file handle that we can read and write to or it may fail if it doesnt exist, or not having the correct permissions
|
||||
|
||||
`File::open` function needs a way to tell us whether it succeeded or failed hence the use of `Result` enum which conveys this message of failure or success
|
||||
|
||||
When `File::open` succeeds, the value in `greeting_file_result` will be an instance of `Ok` that contains a file handle
|
||||
When it fails the value in `greeting_file_result` will be an intance of `Er` that contains mroe info about the kind o error that occurred
|
||||
|
||||
|
17
RustBrock.code-workspace
Normal file
17
RustBrock.code-workspace
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"folders": [
|
||||
{
|
||||
"path": "."
|
||||
}
|
||||
],
|
||||
"settings": {
|
||||
"[markdown]": {
|
||||
"editor.quickSuggestions": {
|
||||
"other": false, // Disable other suggestions
|
||||
"comments": false,
|
||||
"strings": true // Enable suggestions for strings (including links)
|
||||
},
|
||||
"editor.suggestOnTriggerCharacters": true // Keep suggestion on trigger characters (like '[' for links)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user