comply with trainings architecturee
This commit is contained in:
parent
6473f90590
commit
dfd57364e0
6
subject_text/errors/index.md
Normal file
6
subject_text/errors/index.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
name = "Errors"
|
||||
difficulty = 1
|
||||
exercises = ["options"]
|
||||
---
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
---
|
||||
name = "Options"
|
||||
difficulty = 1
|
||||
exercises = [matching]
|
||||
file = "src/errors/option.rs"
|
||||
---
|
||||
|
||||
Sometimes, a function can fail to compute a value simply because the value you asked simply does not exist. For example, when you try to access a collection at a wrong index, or when you want to divide by zero.
|
||||
@ -15,14 +14,14 @@ To check for the existence or absence of value, we use the `Option` type. `Opti
|
||||
|
||||
In order to create values of type `Option<T>`, we just name the variant we want, and if needed, we give the variant a value:
|
||||
|
||||
```example
|
||||
```rust
|
||||
let some_one = Some(1);
|
||||
let nothing: Option<i32> = None;
|
||||
```
|
||||
|
||||
To match against an `Option` value, you can use pattern matching:
|
||||
|
||||
```example
|
||||
```rust
|
||||
let array = [1, 2, 3];
|
||||
let element: Option<&i32> = array.first();
|
||||
|
||||
@ -55,3 +54,11 @@ pub fn get_or_panic(opt: Option<i32>) -> i32 {
|
||||
unimplemented!()
|
||||
}
|
||||
```
|
||||
|
||||
```example
|
||||
fn main() {
|
||||
dbg!(is_some(&None)); // false
|
||||
dbg!(get_or_default(Some(5), 1)); // 5
|
||||
dbg!(get_or_panic(Some(2))); // 2
|
||||
}
|
||||
```
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
---
|
||||
revision = 0.1.0
|
||||
parts = [errors]
|
||||
revision = "0.1.0"
|
||||
parts = ["errors"]
|
||||
---
|
||||
|
||||
When it comes to programming, it's all fun and games until the real world comes in and sends weird unexpected inputs to your little protege. So you better handle those cases as best as you can. There are 3 main ways of handling errors.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user