From a6eeda7f49ddb8005acbeb92c4141fe6a3631b51 Mon Sep 17 00:00:00 2001 From: darkicewolf50 <95242911+darkicewolf50@users.noreply.github.com> Date: Tue, 21 Jan 2025 21:41:38 +0000 Subject: [PATCH] started ch9.2 --- Error Handling.md | 43 ++++++++++++++++++++++++++++++++++++++++ RustBrock.code-workspace | 17 ++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 RustBrock.code-workspace diff --git a/Error Handling.md b/Error Handling.md index 785168c..54ba1f3 100644 --- a/Error Handling.md +++ b/Error Handling.md @@ -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 { + 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` +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 + diff --git a/RustBrock.code-workspace b/RustBrock.code-workspace new file mode 100644 index 0000000..f5aca14 --- /dev/null +++ b/RustBrock.code-workspace @@ -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) + } + } +}