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
- Mainly used with test assertions and ensure runtime conditions true.
17
-
-`assert!`, `assert_eq!` and `assert_ne!`: Standard runtime assertions evaluated in both debug and release builds.
18
-
-`debug_assert!`, `debug_assert_eq!` and `debug_assert_ne!`: Mirrors to standard assertions that run only in debug builds and are omitted from release builds.
- Non-prelude assertions to replace `assert!(matches!(value, pattern))`
24
+
20
25
- Rust cleans up memory by destroying variables on the stack in reverse order after a panic occurs. This process is called Unwinding.
21
26
22
27
## `panic!`
23
28
24
-
Mainly used with unrecoverable errors.
25
-
26
-
In single threaded applications, the program will print an error message, clean up its memory with unwinding, and crash at runtime. In multithreaded applications, a panic affects only on the thread that occurred.
29
+
- Mainly used with unrecoverable errors.
30
+
- In single threaded applications, the program will print an error message, clean up its memory with unwinding, and crash at runtime. In multithreaded applications, a panic affects only on the thread that occurred.
27
31
28
32
### Default Message
29
33
@@ -100,8 +104,7 @@ fn main() {
100
104
101
105
## `todo!` and `unimplemented!`
102
106
103
-
The difference is quite semantic.
104
-
107
+
The difference is quite semantic,\
105
108
`todo!` indicates that we plan to add the code later, while `unimplemented!` only means the code is unimplemented.
106
109
107
110
```rust
@@ -185,7 +188,10 @@ fn main() {
185
188
186
189
## Assertions
187
190
188
-
Similar to the panic!, this also support custom messages and values.
191
+
- Mainly used with test assertions and ensure runtime conditions true.
192
+
- `assert*`: Standard runtime assertions evaluated in both debug and release builds.
193
+
- `debug_assert*`: Mirrors to standard assertions that run only in debug builds and are omitted from release builds.
194
+
- Similar to the `panic!`, these also support custom messages and values.
189
195
190
196
### `assert!` & `debug_assert!`
191
197
@@ -289,12 +295,14 @@ fn main() {
289
295
290
296
Ensures an expression matches a given pattern.
291
297
298
+
> [!tip]
299
+
> Non-prelude assertions to replace `assert!(matches!(value, pattern))`
300
+
292
301
```rust
293
302
fn main() {
294
303
let a = Some(10);
295
304
std::assert_matches!(a, Some(x) if x > 20); // or debug_assert_matches!
296
305
}
297
-
// 💡 assert_matches! and debug_assert_matches! are not in Rust preludes
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="o">|</span><span class="w"></span><span class="o">-</span><span class="w"></span><span class="n">value</span><span class="w"></span><span class="n">moved</span><span class="w"></span><span class="n">here</span></span></span></code></pre></div><p>This is because of Ownership, which is used to achieve Rust&rsquo;s memory safety.</p></description></item><item><title>Panicking</title><link>https://learning-rust.github.io/docs/panicking/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://learning-rust.github.io/docs/panicking/</guid><description><ul>
607
-
<li>Panicking immediately terminates the current thread.
607
+
<li>
608
+
<p>Panicking immediately terminates the current thread.</p>
608
609
<ul>
609
610
<li><code>panic!</code>: Mainly used with unrecoverable errors.
<li>Mainly used with test assertions and ensure runtime conditions true.</li>
632
-
<li><code>assert!</code>, <code>assert_eq!</code> and <code>assert_ne!</code>: Standard runtime assertions evaluated in both debug and release builds.</li>
633
-
<li><code>debug_assert!</code>, <code>debug_assert_eq!</code> and <code>debug_assert_ne!</code>: Mirrors to standard assertions that run only in debug builds and are omitted from release builds.</li>
<li><code>assert_eq!</code>/ <code>debug_assert_eq!</code> and <code>assert_ne!</code>/ <code>debug_assert_ne!</code>: Ensures two expressions are equal or not equal.
<li>Non-prelude assertions to replace <code>assert!(matches!(value, pattern))</code></li>
635
647
</ul>
636
648
</li>
637
649
</ul>
638
650
</li>
639
-
<li>Rust cleans up memory by destroying variables on the stack in reverse order after a panic occurs. This process is called Unwinding.</li>
<p>Mainly used with unrecoverable errors.</p></description></item><item><title>Primitive Data Types</title><link>https://learning-rust.github.io/docs/primitive-data-types/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://learning-rust.github.io/docs/primitive-data-types/</guid><description><h2 id="bool">bool</h2>
652
+
</li>
653
+
<li>
654
+
<p>Rust cleans up memory by destroying variables on the stack in reverse order after a panic occurs. This process is called Unwinding.</p></description></item><item><title>Primitive Data Types</title><link>https://learning-rust.github.io/docs/primitive-data-types/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://learning-rust.github.io/docs/primitive-data-types/</guid><description><h2 id="bool">bool</h2>
0 commit comments