Skip to content

Commit d42606c

Browse files
Update e3.option-and-result.md typos/grammar (#66)
* Update e3.option-and-result.md typos/grammar * Update e3.option-and-result.md --------- Co-authored-by: Dumindu Madunuwan <[email protected]>
1 parent 3fd076c commit d42606c

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

content/en/docs/e3.option-and-result.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ slug: option-and-result
55

66
## Why Option and Result?
77

8-
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.
8+
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.
99

1010
> 💭 In the previous sections, we have discussed about the basics of [enums](/docs/enums), [generics](/docs/generics) and [`Result` & `Option` types](/docs/generics/#generalizing-enums).
1111
@@ -32,12 +32,14 @@ enum Result<T, E> { // T and E are generics. T can contain any type of value, E
3232
## Basic usages of Option
3333

3434
When writing a function or data type,
35+
3536
- if an **argument** of the function is optional,
36-
- If the function is non-void and if the output it **returns** can be empty,
37-
- If the value, of a **property of the data type** can be empty,
38-
We have to use their data type as an `Option` type
37+
- if the function is non-void and if the output it **returns** can be empty,
38+
- if the value of a **property of the data type** can be empty,
39+
40+
we have to use their data type as an `Option` type.
3941

40-
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>`.
42+
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>`.
4143

4244
```rust
4345
fn get_an_optional_value() -> Option<&str> {
@@ -50,7 +52,7 @@ fn get_an_optional_value() -> Option<&str> {
5052
}
5153
```
5254

53-
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.
55+
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.
5456

5557
```rust
5658
struct Name {
@@ -60,7 +62,7 @@ struct Name {
6062
}
6163
```
6264

63-
💭 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).
65+
💭 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).
6466

6567
```rust
6668
use std::env;
@@ -96,7 +98,7 @@ fn main() {
9698

9799
## Basic usages of Result
98100

99-
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>`.
101+
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>`.
100102

101103
```rust
102104
fn function_with_error() -> Result<u64, String> {
@@ -109,7 +111,7 @@ fn function_with_error() -> Result<u64, String> {
109111
}
110112
```
111113

112-
💭 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).
114+
💭 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).
113115

114116
```rust
115117
use std::env;
@@ -141,7 +143,7 @@ fn main() {
141143

142144
## ok(), err() for Result types
143145

144-
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**.
146+
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**.
145147

146148
```rust
147149
fn main() {

0 commit comments

Comments
 (0)