You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/en/docs/e3.option-and-result.md
+12-10
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ slug: option-and-result
5
5
6
6
## Why Option and Result?
7
7
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.
9
9
10
10
> 💭 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).
11
11
@@ -32,12 +32,14 @@ enum Result<T, E> { // T and E are generics. T can contain any type of value, E
32
32
## Basic usages of Option
33
33
34
34
When writing a function or data type,
35
+
35
36
- 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.
39
41
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>`.
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.
54
56
55
57
```rust
56
58
structName {
@@ -60,7 +62,7 @@ struct Name {
60
62
}
61
63
```
62
64
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).
64
66
65
67
```rust
66
68
usestd::env;
@@ -96,7 +98,7 @@ fn main() {
96
98
97
99
## Basic usages of Result
98
100
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>`.
💭 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).
113
115
114
116
```rust
115
117
usestd::env;
@@ -141,7 +143,7 @@ fn main() {
141
143
142
144
## ok(), err() for Result types
143
145
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**.
0 commit comments