Skip to content

Commit 145f347

Browse files
committed
Error Handling
1 parent ca2bc12 commit 145f347

37 files changed

Lines changed: 183 additions & 166 deletions
64.6 KB
Loading
55.7 KB
Loading

content/en/docs/e1-error-typos.jpg

47.4 KB
Loading
63.6 KB
Loading

content/en/docs/e1.smart-compiler.md

Lines changed: 72 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,109 +3,106 @@ title: Smart Compiler
33
slug: smart-compiler
44
---
55

6-
> [!caution]
7-
> This needs a refresh!
6+
## Compile-time Errors
87

9-
## Why Compiler?
8+
Rust focuses on strict code hygiene, and the compiler generates a highly specific set of warnings and errors to make it easier to identify and solve them.
109

11-
The Rust compiler does the most significant job to prevent errors in Rust programs. It **analyzes the code at compile-time** and issues warnings, if the code does not follow memory management rules or lifetime annotations correctly.
10+
### Typos
1211

13-
For example,
1412
```rust
15-
#[allow(unused_variables)] //💡 A lint attribute used to suppress the warning; unused variable: `b`
13+
struct Person {
14+
name: String,
15+
company_name: String,
16+
}
17+
18+
fn main() {
19+
let steve = Person {
20+
name: "Steve Jobs".to_string(),
21+
company_name: "Apple".to_string(),
22+
};
23+
24+
// Destructuring fields' values to a and b
25+
let Person {fname: a, company_name: b} = steve;
26+
27+
println!("{a} {b}");
28+
}
29+
```
30+
31+
> [!caution] Compile-time Error
32+
> ![Compile-time Error: Typos](/docs/e1-error-typos.jpg)
33+
34+
### Ownership
35+
36+
```rust
37+
#![allow(unused)] //💡 A lint attribute used to suppress the warning; unused variable: `b`
1638
fn main() {
1739
let a = vec![1, 2, 3];
1840
let b = a;
1941

2042
println!("{:?}", a);
2143
}
44+
```
2245

46+
> [!caution] Compile-time Error
47+
> ![Compile-time Error: Ownership](/docs/e1-error-ownership.jpg)
2348
24-
// ------ Compile-time error ------
25-
error[E0382]: use of moved value: `a`
26-
--> src/main.rs:6:22
27-
|
28-
3 | let b = a;
29-
| - value moved here
30-
4 |
31-
5 | println!("{:?}", a);
32-
| ^ value used here after move
33-
|
34-
= note: move occurs because `a` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
49+
### Lifetimes
3550

36-
error: aborting due to previous error
37-
For more information about this error, try `rustc --explain E0382`.
51+
```rust
52+
fn main() {
53+
let steve = Person{fname: "Steve", lname: "Jobs"};
54+
steve.intro();
55+
}
56+
57+
struct Person {
58+
fname: &str,
59+
lname: &str,
60+
}
3861

39-
// ⭐ instead using #[allow(unused_variables)], consider using "let _b = a;" in line 4.
40-
// Also you can use "let _ =" to completely ignore return values
62+
impl Person {
63+
fn intro(&self) {
64+
println!("Hello! I am {} {}.", self.fname, self.lname)
65+
}
66+
}
4167
```
4268

43-
> [!recap]
44-
> In the previous sections, we have discussed memory management concepts like [ownership](/docs/ownership), [borrowing](/docs/borrowing), [lifetimes](/docs/lifetimes) and etc.
69+
> [!caution] Compile-time Error
70+
> ![Compile-time Error: Lifetimes](/docs/e1-error-lifetimes.jpg)
4571
46-
Rust compiler checks not only issues related with lifetimes or memory management and also common coding mistakes, like the following code.
72+
### Concurrency
4773

4874
```rust
49-
struct Color {
50-
r: u8,
51-
g: u8,
52-
b: u8,
53-
}
75+
use std::{rc::Rc, thread};
5476

5577
fn main() {
56-
let yellow = Color {
57-
r: 255,
58-
g: 255,
59-
d: 0,
60-
};
61-
62-
println!("Yellow = rgb({},{},{})", yellow.r, yellow.g, yellow.b);
63-
}
78+
let data = Rc::new(vec![1, 2, 3]);
6479

80+
let value1 = Rc::clone(&data);
81+
let handle1 = thread::spawn(move || {
82+
println!("{:?}", &value1);
83+
});
6584

66-
// ------------ Compile-time error ------------
67-
error[E0560]: struct `Color` has no field named `d`
68-
--> src/main.rs:11:9
69-
|
70-
11 | d: 0,
71-
| ^ field does not exist - did you mean `b`?
85+
let value2 = Rc::clone(&data);
86+
let handle2 = thread::spawn(move || {
87+
println!("{:?}", &value2);
88+
});
7289

73-
error: aborting due to previous error
74-
For more information about this error, try `rustc --explain E0560`.
90+
handle1.join().unwrap();
91+
handle2.join().unwrap();
92+
}
7593
```
7694

77-
## Explain Error Codes
95+
> [!caution] Compile-time Error
96+
> ![Compile-time Error: Concurrency](/docs/e1-error-concurrency.jpg)
7897
79-
Above error messages are very descriptive and we can easily see where is the error. But while we can not identify the issue via the error message, **`rustc --explain`** commands help us **to identify the error type and how to solve** it, by showing **simple code samples** which express the same problem and the solution we have to use.
98+
## Error Codes
8099

81-
For example, `rustc --explain E0571` shows the following output in the console.
100+
- Rust's error codes consist of the letter **E** followed by four digits.
101+
- Each code corresponds to a specific kind of error, and `rustc --explain <CODE>` provides more details to help to identify the error.
102+
- The same explanations are also available in the [Rust Error Codes Index](https://doc.rust-lang.org/error_codes/error-index.html).
82103

83-
```rust
84-
A `break` statement with an argument appeared in a non-`loop` loop.
104+
For example, `rustc --explain E0571` shows the following output in the console, and the same content is also available at https://doc.rust-lang.org/error_codes/E0571.html.
85105

86-
Example of erroneous code:
87-
```
88-
let result = while true {
89-
if satisfied(i) {
90-
break 2*i; // error: `break` with value from a `while` loop
91-
}
92-
i += 1;
93-
};
94-
```
95-
96-
The `break` statement can take an argument (which will be the value of the loop
97-
expression if the `break` statement is executed) in `loop` loops, but not
98-
`for`, `while`, or `while let` loops.
99-
100-
Make sure `break value;` statements only occur in `loop` loops:
101-
```
102-
let result = loop { // ok!
103-
if satisfied(i) {
104-
break 2*i;
105-
}
106-
i += 1;
107-
};
108-
```
109-
```
106+
> [!search]
107+
> ![Explain Error Codes](/docs/e1-explain-error-codes.jpg)
110108
111-
💡 Also you can read the same explanations via [Rust Compiler Error Index](https://doc.rust-lang.org/error_codes/error-index.html). For example to check the explanation of `E0571` error, you can use https://doc.rust-lang.org/error-index.html#E0571.

0 commit comments

Comments
 (0)