Skip to content

Commit 32e0a8d

Browse files
author
Dana Binkley
committed
Merge branch 'mr/396-rework-rust-essentials-module-error-handling' into 'master'
Resolve "Rework Rust Essentials module: Error handling" Closes #396 See merge request feng/training/material!530
2 parents 312863b + 6f4ef74 commit 32e0a8d

19 files changed

Lines changed: 897 additions & 498 deletions

courses/rust_essentials/200_error_handling.rst

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ Error Handling
3232

3333
.. container:: PRELUDE END
3434

35-
.. include:: 200_error_handling/01_panics.rst
36-
.. include:: 200_error_handling/02_result.rst
37-
.. include:: 200_error_handling/03_try.rst
38-
.. include:: 200_error_handling/04_try_conversions.rst
39-
.. include:: 200_error_handling/05_error.rst
40-
.. include:: 200_error_handling/06_thiserror.rst
41-
.. include:: 200_error_handling/07_anyhow.rst
42-
.. include:: 200_error_handling/99_lab.rst
35+
.. include:: 200_error_handling/00-introduction.rst
36+
.. include:: 200_error_handling/01-overview.rst
37+
.. include:: 200_error_handling/02-panic.rst
38+
.. include:: 200_error_handling/03-result.rst
39+
.. include:: 200_error_handling/04-try_operator.rst
40+
.. include:: 200_error_handling/05-try_conversions.rst
41+
.. include:: 200_error_handling/06-error_trait.rst
42+
.. include:: 200_error_handling/07-thiserror.rst
43+
.. include:: 200_error_handling/08-anyhow.rst
44+
.. include:: 200_error_handling/88-error_handling.lab.rst
45+
.. include:: 200_error_handling/99-summary.rst
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
==============
2+
Introduction
3+
==============
4+
5+
----------------
6+
Topics Covered
7+
----------------
8+
9+
* **Errors**
10+
11+
* Unrecoverable errors - :rust:`panic!`
12+
* Recoverable Errors - :rust:`Result`
13+
14+
* **Try Operator**
15+
16+
* Error propagation
17+
* Converting error traits
18+
19+
* **Error Trait**
20+
21+
* Defining error implementations
22+
23+
* **Simplified Error Handling**
24+
25+
* Creating Errors Through :rust:`derive` - :rust:`thiserror`
26+
* :rust:`anyhow`
27+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
==========
2+
Overview
3+
==========
4+
5+
-----------------------
6+
Rust Error Philosophy
7+
-----------------------
8+
9+
* Errors are data, not drama!
10+
11+
* Failures are explicit values
12+
13+
* Not hidden control flow
14+
15+
* Errors are visible in function signatures
16+
* Compiler ensures they are handled
17+
18+
* No access to invalid data
19+
20+
* Failures do not show up mysteriously
21+
22+
* Visible and enforced
23+
24+
.. list-table::
25+
:header-rows: 1
26+
27+
* - **Language Style**
28+
- **Error Visibility**
29+
30+
* - Exceptions
31+
- Hidden control flow
32+
33+
* - Error codes
34+
- Easy to ignore
35+
36+
* - Error wrappers (e.g., :rust:`Result`)
37+
- Explicit and enforced
38+
39+
--------------------------------------
40+
Expected Errors vs. Logic Violations
41+
--------------------------------------
42+
43+
* Some errors can be handled by the code
44+
45+
* File not found
46+
* Network timeout
47+
* Invalid user input
48+
49+
* Sometimes we just need to reboot the system
50+
51+
* Impossible state reached
52+
* Violated assumptions / invariant
53+
* Logic error or other bug
54+
55+
.. note::
56+
57+
Rust separates bugs from expected failures

courses/rust_essentials/200_error_handling/01_panics.rst

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
=======
2+
Panic
3+
=======
4+
5+
---------------------
6+
Unrecoverable Error
7+
---------------------
8+
9+
* Runtime errors are not recoverable
10+
11+
* Failed bounds checks
12+
13+
* Accessing :rust:`v[100]` on a 3-item vector
14+
15+
* Logic problems
16+
17+
* Failed :rust:`assert!` or :rust:`debug_assert!`
18+
19+
* Call :rust:`panic!` when logic indicates unrecoverable error
20+
21+
* Triggers thread shut-down
22+
* Configurable - either unwind stack or abort
23+
24+
------------------------------
25+
What Happens During a Panic?
26+
------------------------------
27+
28+
* **Stack unwinding** (default)
29+
30+
* Rust walks back up the stack and "cleans up"
31+
32+
* Runs :rust:`drop` for all objects in scope
33+
34+
* Useful when working with hardware or multiple processes
35+
36+
* Reset hardware flags
37+
* Release any locks
38+
39+
* **Aborting** (configurable)
40+
41+
* Instantly stops the program
42+
* Results in smaller binary size
43+
44+
* No cleanup code
45+
46+
--------------
47+
Code Example
48+
--------------
49+
50+
**Bounds error**
51+
52+
.. code:: rust
53+
:number-lines: 1
54+
55+
fn main() {
56+
let my_vector = vec![10, 20, 30];
57+
58+
println!("{}", my_vector[100]);
59+
}
60+
61+
:error:`thread 'main' (32) panicked at src/main.rs:4:21:`
62+
63+
:error:`index out of bounds: the len is 3 but the index is 100`
64+
65+
**Manual panic**
66+
67+
.. code:: rust
68+
:number-lines: 1
69+
70+
fn main() {
71+
let my_vector = vec![10, 20, 30];
72+
73+
if my_vector.len() < 10 {
74+
panic!("Vector is too short!");
75+
}
76+
}
77+
78+
:error:`thread 'main' (12) panicked at src/main.rs:5:9:`
79+
80+
:error:`Vector is too short!`
81+
82+
----------------
83+
When to Panic?
84+
----------------
85+
86+
* **Prototyping**
87+
88+
* :rust:`unwrap()` or :rust:`expect()` for quick coding
89+
90+
* Replace with proper error handling later
91+
92+
* **Infallible logic** (logic guarantees)
93+
94+
* In a state that should never occur
95+
* Panic indicates a bug
96+
97+
* **Library boundaries**
98+
99+
* Libraries should return :rust:`Result`
100+
101+
* User decides how to handle the error
102+
103+
* Panic if API **contract** is violated
104+
105+
* E.g., passing an empty list to a function that requires items

courses/rust_essentials/200_error_handling/02_result.rst

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)