|
193 | 193 | Otherwise, `if let Ok(..) = ..` will be chosen
|
194 | 194 | to handle the **success** case.
|
195 | 195 |
|
196 |
| - `if let` can be used only for `Result` and `Option` data tyes. |
| 196 | + `if let` can be used only for `Result` and `Option` data types. |
197 | 197 | code: >-
|
198 | 198 | https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn+div%28x%3A+f64%2C+y%3A+f64%29+-%3E+Result%3Cf64%2C+String%3E+%7B%0A++++if+y+%3D%3D+0.0+%7B%0A++++++++return+Err%28String%3A%3Afrom%28%22Invalid+operation%3A+division+by+zero.%22%29%29%3B%0A++++%7D%0A++++return+Ok%28x+%2F+y%29%3B%0A%7D%0A%0A%0Afn+main%28%29+%7B%0A++++if+let+Ok%28res%29+%3D+div%2810.0%2C+1.0%29+%7B%0A++++++++println%21%28%22%7B%7D%22%2C+res%29%3B++++++++%2F%2F+will+be+displayed%0A++++%7D%0A++++%0A++++if+let+Err%28err%29+%3D+div%2810.0%2C+1.0%29+%7B%0A++++++++println%21%28%22%7B%7D%22%2C+err%29%3B++++++++%2F%2F+will+NOT+be+displayed%0A++++%7D%0A++++%0A%0A++++if+let+Ok%28res%29+%3D+div%2810.0%2C+0.0%29+%7B%0A++++++++println%21%28%22%7B%7D%22%2C+res%29%3B++++++++%2F%2F+will+be+displayed%0A++++%7D%0A++++if+let+Err%28err%29+%3D+div%2810.0%2C+0.0%29+%7B%0A++++++++println%21%28%22%7B%7D%22%2C+err%29%3B++++++++%2F%2F+will+NOT+be+displayed%0A++++%7D%0A%7D%0A
|
199 | 199 | - title: Extracting a Result
|
200 | 200 | content_markdown: >
|
201 | 201 | Another brilliant way to extract a `Result<T, E> { Ok(T), Err(E), }`
|
202 | 202 | is to use the functions `.ok()` and `.err()`, representing the specific variant.
|
203 | 203 |
|
204 |
| - These functions will return an `Option`: |
205 |
| - - `Some` if the Result function had something to return, for results like `Result<i32, i32>`. |
206 |
| - - `None` if the Result function returned either a `Err(())` or an `Ok(())`, for results like `Result<(), ()>`, `Result<i32, ()>` or `Result<(), i32>` |
| 204 | + Let's assume we are working with a function whose return type is `Result`, just like in the example. |
| 205 | + Using `.ok()` and `.err()` on it will return an `Option`: |
| 206 | + - `Some` if the original function had something to return, for results like `Result<i32, i32>`. |
| 207 | + - `None` if the original function returned either a `Err(())` or an `Ok(())`, for results like `Result<(), ()>`, `Result<i32, ()>` or `Result<(), i32>` |
| 208 | +
|
207 | 209 | code: >-
|
208 | 210 | https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn+div1%28x%3A+f64%2C+y%3A+f64%29+-%3E+Result%3Cf64%2C+String%3E+%7B%0A++++if+y+%3D%3D+0.0+%7B%0A++++++++return+Err%28String%3A%3Afrom%28%22Invalid+operation%3A+division+by+zero.%22%29%29%3B%0A++++%7D%0A++++return+Ok%28x+%2F+y%29%3B%0A%7D%0A%0A%0Afn+div2%28x%3A+f64%2C+y%3A+f64%29+-%3E+Result%3Cf64%2C+%28%29%3E+%7B%0A++++if+y+%3D%3D+0.0+%7B%0A++++++++%2F%2F+notice+that+the+error+returns+%60None%60%0A++++++++%2F%2F+therefore+will+be+pattern-matched+using+%60%28%29%60%0A++++++++return+Err%28%28%29%29%3B%0A++++%7D%0A++++return+Ok%28x+%2F+y%29%3B%0A%7D%0A%0Afn+main%28%29+%7B%0A++++let+good1+%3D+div1%2810.0%2C+1.0%29%3B%0A++++let+bad1+%3D+div1%2810.0%2C+0.0%29%3B%0A++++%0A++++%2F%2F+Using+.ok%28%29+will+extract+the+Ok+variant%0A++++%2F%2F+.ok%28%29+returns+an+Option%3A+Some%0A++++if+let+Some%28value%29+%3D+good1.ok%28%29+%7B%0A++++++++println%21%28%22Result+%28Ok%29%3A+%7B%7D%22%2C+value%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22Result+%28Err%29%22%29%3B%0A++++%7D%0A%0A++++%2F%2F+Using+.err%28%29+to+extract+the+Err+variant%0A++++%2F%2F+.err%28%29+returns+an+Option%3A+Some%0A++++if+let+Some%28err%29+%3D+bad1.err%28%29+%7B%0A++++++++println%21%28%22Error%3A+%7B%7D%22%2C+err%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22No+error+%28Ok%29%22%29%3B%0A++++%7D%0A++++%0A++++%0A++++let+good2+%3D+div2%2810.0%2C+1.0%29%3B%0A++++let+bad2+%3D+div2%2810.0%2C+0.0%29%3B%0A++++%0A++++%2F%2F+Using+.ok%28%29+will+extract+the+Ok+variant%0A++++%2F%2F+.ok%28%29+returns+an+Option%3A+Some%0A++++if+let+Some%28value%29+%3D+good2.ok%28%29+%7B%0A++++++++println%21%28%22Result+%28Ok%29%3A+%7B%7D%22%2C+value%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22Result+%28Err%29%22%29%3B%0A++++%7D%0A%0A++++%2F%2F+Using+.err%28%29+to+extract+the+Err+variant%0A++++%2F%2F+.err%28%29+returns+an+Option%3A+None%0A++++if+let+Some%28%28%29%29+%3D+bad2.err%28%29+%7B%0A++++++++println%21%28%22Error+occured%22%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22No+error+%28Ok%29%22%29%3B%0A++++%7D%0A%7D%0A
|
209 | 211 | - title: About panic!
|
210 | 212 | content_markdown: >
|
211 | 213 | In Rust, `panic!` is a macro used to stop the execution of the program
|
212 |
| - without a recoverable error. When a panic occurs, the program immediately |
213 |
| - stop, unwinding the stack and cleaning up resources along the way. |
| 214 | + without a recoverable error. When panic occurs, the program immediately |
| 215 | + stops, unwinding the stack and cleaning up resources along the way. |
214 | 216 |
|
215 | 217 | Moreover, the code instructions written after `panic!` will no longer be executed.
|
216 | 218 |
|
|
261 | 263 | iterators were designed with a very powerful function, `.map()`.
|
262 | 264 |
|
263 | 265 | `.map()` and `.iter()` are methods provided by iterators and are commonly
|
264 |
| - used in working with collection (vectors and String, for instance). |
| 266 | + used in working with collections (vectors and String, for instance). |
265 | 267 |
|
266 | 268 | The function `.iter()` iterates over a reference to each element of a collection,
|
267 | 269 | generating a sequence of elements.
|
|
0 commit comments