Skip to content

Update e3.option-and-result.md typos/grammar #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions content/en/docs/e3.option-and-result.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand All @@ -32,12 +32,14 @@ enum Result<T, E> { // 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> {
Expand All @@ -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 {
Expand All @@ -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<PathBuf>`](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<PathBuf>`](https://doc.rust-lang.org/std/path/struct.PathBuf.html).

```rust
use std::env;
Expand Down Expand Up @@ -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<u64, String>`.
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<u64, String>`.

```rust
fn function_with_error() -> Result<u64, String> {
Expand All @@ -109,7 +111,7 @@ fn function_with_error() -> Result<u64, String> {
}
```

💭 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<String, VarError>`](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<String, VarError>`](https://doc.rust-lang.org/std/env/enum.VarError.html).

```rust
use std::env;
Expand Down Expand Up @@ -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<T>` and `Err<E>` values of a **`Result` type to `Option` types**.
In addition to that, Rust provides `ok()` and `err()` for `Result` types. They convert the `Ok<T>` and `Err<E>` values of a **`Result` type to `Option` types**.

```rust
fn main() {
Expand Down