diff --git a/content/en/docs/e3.option-and-result.md b/content/en/docs/e3.option-and-result.md index b909851..5856825 100644 --- a/content/en/docs/e3.option-and-result.md +++ b/content/en/docs/e3.option-and-result.md @@ -5,7 +5,7 @@ slug: option-and-result ## Why Option and Result? -Many languages use **`null`\ `nil`\ `undefined` types** to represent empty outputs, and **`Exceptions`** to handle errors. Rust skips using both, especially to prevent issues like **null pointer exceptions, sensitive data leakages through exceptions** and etc. Instead, Rust provides two special **generic enums**;`Option` and `Result` to deal with above cases. +Many languages use **`null`\ `nil`\ `undefined` types** to represent empty outputs, and **`Exceptions`** to handle errors. Rust skips using both, especially to prevent issues like **null pointer exceptions, sensitive data leakages through exceptions**, etc. Instead, Rust provides two special **generic enums**;`Option` and `Result` to deal with above cases. > 💭 In the previous sections, we have discussed about the basics of [enums](b3.enums.html), [generics](b4.generics.html) and [`Result` & `Option` types](b4.generics.html#Generalizing-enums). @@ -32,12 +32,14 @@ enum Result { // T and E are generics. T can contain any type of value, E ## Basic usages of Option When writing a function or data type, + - if an **argument** of the function is optional, -- If the function is non-void and if the output it **returns** can be empty, -- If the value, of a **property of the data type** can be empty, -We have to use their data type as an `Option` type +- if the function is non-void and if the output it **returns** can be empty, +- if the value of a **property of the data type** can be empty, + +we have to use their data type as an `Option` type. -For example, if the function outputs a `&str` value and the output can be empty, the return type of the function should set as `Option<&str>`. +For example, if the function outputs a `&str` value and the output can be empty, the return type of the function should be set as `Option<&str>`. ```rust fn get_an_optional_value() -> Option<&str> { @@ -50,7 +52,7 @@ fn get_an_optional_value() -> Option<&str> { } ``` -Same way, if the value of a property of a data type can be empty or optional like the `middle_name` of `Name` data type in the following example, we should set its data type as an `Option` type. +In the same way, if the value of a property of a data type can be empty or optional like the `middle_name` of the `Name` data type in the following example, we should set its data type as an `Option` type. ```rust struct Name { @@ -60,7 +62,7 @@ struct Name { } ``` -💭 As you know, we can use pattern matching to catch the relevant return type (`Some`/ `None`) via `match`. There is a function to get the current user’s home directory in **`std::env`** as **[`home_dir()`](https://doc.rust-lang.org/std/env/fn.home_dir.html)**. Because of all users doesn’t have a home directory in the systems like Linux, home directory of the user can be optional. So it returns an `Option` type; [`Option`](https://doc.rust-lang.org/std/path/struct.PathBuf.html). +💭 As you know, we can use pattern matching to catch the relevant return type (`Some`/ `None`) via `match`. There is a function in **`std::env`** called **[`home_dir()`](https://doc.rust-lang.org/std/env/fn.home_dir.html)** to get the current user's home directory. However, not all users have a home directory in systems like Linux, so the home directory of a user can be optional. So it returns an `Option` type; [`Option`](https://doc.rust-lang.org/std/path/struct.PathBuf.html). ```rust use std::env; @@ -96,7 +98,7 @@ fn main() { ## Basic usages of Result -If a function can produce an error, we have to use a `Result` type by **combining the data type of the valid output and the data type of the error**. For example, if the data type of the valid output is `u64` and error type is `String`, return type should be `Result`. +If a function can produce an error, we have to use a `Result` type by **combining the data type of the valid output and the data type of the error**. For example, if the data type of the valid output is `u64` and error type is `String`, the return type should be `Result`. ```rust fn function_with_error() -> Result { @@ -109,7 +111,7 @@ fn function_with_error() -> Result { } ``` -💭 As you know, we can use the pattern matching to catch the relevant return types (`Ok`/`Err`) via `match`. There is a function to fetch the value of any environment variable in **`std::env`** as **[`var()`](https://doc.rust-lang.org/std/env/fn.var.html)** . Its input is the environment variable name. This can produce an error, if we passes a wrong environment variable or the program can not extract the value of the environment variable while running. So, its return type is a `Result` type; [`Result`](https://doc.rust-lang.org/std/env/enum.VarError.html). +💭 As you know, we can use the pattern matching to catch the relevant return types (`Ok`/`Err`) via `match`. There is a function to fetch the value of any environment variable in **`std::env`** called **[`var()`](https://doc.rust-lang.org/std/env/fn.var.html)**. Its input is the environment variable name. This can produce an error if we pass a wrong environment variable or the program cannot extract the value of the environment variable while running. So, its return type is a `Result` type; [`Result`](https://doc.rust-lang.org/std/env/enum.VarError.html). ```rust use std::env; @@ -141,7 +143,7 @@ fn main() { ## ok(), err() for Result types -In addition to that Rust provides `ok()` and `err()` for `Result` types. They convert the `Ok` and `Err` values of a **`Result` type to `Option` types**. +In addition to that, Rust provides `ok()` and `err()` for `Result` types. They convert the `Ok` and `Err` values of a **`Result` type to `Option` types**. ```rust fn main() {