Skip to content

Commit 78d0caf

Browse files
committed
Assertions
1 parent 5577142 commit 78d0caf

6 files changed

Lines changed: 65 additions & 34 deletions

File tree

content/en/docs/e2.panicking.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ slug: panicking
1414
- Default panic message/prefix: `internal error: entered unreachable code`
1515
- Assertions
1616
- 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.
19-
- Default panic message: `assertion failed:` or ```assertion `left != right` failed```
17+
- `assert!`/ `debug_assert!`: Ensures a condition is true.
18+
- Default panic message: `assertion failed`
19+
- `assert_eq!`/ `debug_assert_eq!` and `assert_ne!`/ `debug_assert_ne!`: Ensures two expressions are equal or not equal.
20+
- Default panic message/prefix: ```assertion `left != right` failed```
21+
- `assert_matches!`/ `debug_assert_matches!`: Ensures an expression matches a given pattern.
22+
- Default panic message/prefix: ```assertion `left matches right` failed```
23+
- Non-prelude assertions to replace `assert!(matches!(value, pattern))`
24+
2025
- Rust cleans up memory by destroying variables on the stack in reverse order after a panic occurs. This process is called Unwinding.
2126

2227
## `panic!`
2328

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.
2731

2832
### Default Message
2933

@@ -100,8 +104,7 @@ fn main() {
100104
101105
## `todo!` and `unimplemented!`
102106
103-
The difference is quite semantic.
104-
107+
The difference is quite semantic,\
105108
`todo!` indicates that we plan to add the code later, while `unimplemented!` only means the code is unimplemented.
106109
107110
```rust
@@ -185,7 +188,10 @@ fn main() {
185188
186189
## Assertions
187190
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.
189195
190196
### `assert!` & `debug_assert!`
191197
@@ -289,12 +295,14 @@ fn main() {
289295
290296
Ensures an expression matches a given pattern.
291297
298+
> [!tip]
299+
> Non-prelude assertions to replace `assert!(matches!(value, pattern))`
300+
292301
```rust
293302
fn main() {
294303
let a = Some(10);
295304
std::assert_matches!(a, Some(x) if x > 20); // or debug_assert_matches!
296305
}
297-
// 💡 assert_matches! and debug_assert_matches! are not in Rust preludes
298306
```
299307
300308
> [!Caution] Crash at Runtime

docs/docs/index.xml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,8 @@ __ <a href="https://wiki.haskell.org/Combinator" target="_blank" >wiki.has
604604
</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="k">move</span><span class="w"> </span><span class="n">occurs</span><span class="w"> </span><span class="n">because</span><span class="w"> </span><span class="err">`</span><span class="n">a</span><span class="err">`</span><span class="w"> </span><span class="n">has</span><span class="w"> </span><span class="k">type</span> <span class="err">`</span><span class="n">Point</span><span class="err">`</span><span class="p">,</span><span class="w"> </span><span class="n">which</span><span class="w"> </span><span class="n">does</span><span class="w"> </span><span class="n">not</span><span class="w"> </span><span class="n">implement</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="err">`</span><span class="nb">Copy</span><span class="err">`</span><span class="w"> </span><span class="k">trait</span><span class="w">
605605
</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="mi">9</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">;</span><span class="w">
606606
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;moved&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;here&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is because of Ownership, which is used to achieve Rust&amp;rsquo;s memory safety.&lt;/p&gt;</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>&lt;ul&gt;
607-
&lt;li&gt;Panicking immediately terminates the current thread.
607+
&lt;li&gt;
608+
&lt;p&gt;Panicking immediately terminates the current thread.&lt;/p&gt;
608609
&lt;ul&gt;
609610
&lt;li&gt;&lt;code&gt;panic!&lt;/code&gt;: Mainly used with unrecoverable errors.
610611
&lt;ul&gt;
@@ -629,17 +630,28 @@ __ &lt;a href="https://wiki.haskell.org/Combinator" target="_blank" &gt;wiki.has
629630
&lt;li&gt;Assertions
630631
&lt;ul&gt;
631632
&lt;li&gt;Mainly used with test assertions and ensure runtime conditions true.&lt;/li&gt;
632-
&lt;li&gt;&lt;code&gt;assert!&lt;/code&gt;, &lt;code&gt;assert_eq!&lt;/code&gt; and &lt;code&gt;assert_ne!&lt;/code&gt;: Standard runtime assertions evaluated in both debug and release builds.&lt;/li&gt;
633-
&lt;li&gt;&lt;code&gt;debug_assert!&lt;/code&gt;, &lt;code&gt;debug_assert_eq!&lt;/code&gt; and &lt;code&gt;debug_assert_ne!&lt;/code&gt;: Mirrors to standard assertions that run only in debug builds and are omitted from release builds.&lt;/li&gt;
634-
&lt;li&gt;Default panic message: &lt;code&gt;assertion failed:&lt;/code&gt; or &lt;code&gt;assertion `left != right` failed&lt;/code&gt;&lt;/li&gt;
633+
&lt;li&gt;&lt;code&gt;assert!&lt;/code&gt;/ &lt;code&gt;debug_assert!&lt;/code&gt;: Ensures a condition is true.
634+
&lt;ul&gt;
635+
&lt;li&gt;Default panic message: &lt;code&gt;assertion failed&lt;/code&gt;&lt;/li&gt;
636+
&lt;/ul&gt;
637+
&lt;/li&gt;
638+
&lt;li&gt;&lt;code&gt;assert_eq!&lt;/code&gt;/ &lt;code&gt;debug_assert_eq!&lt;/code&gt; and &lt;code&gt;assert_ne!&lt;/code&gt;/ &lt;code&gt;debug_assert_ne!&lt;/code&gt;: Ensures two expressions are equal or not equal.
639+
&lt;ul&gt;
640+
&lt;li&gt;Default panic message/prefix: &lt;code&gt;assertion `left != right` failed&lt;/code&gt;&lt;/li&gt;
641+
&lt;/ul&gt;
642+
&lt;/li&gt;
643+
&lt;li&gt;&lt;code&gt;assert_matches!&lt;/code&gt;/ &lt;code&gt;debug_assert_matches!&lt;/code&gt;: Ensures an expression matches a given pattern.
644+
&lt;ul&gt;
645+
&lt;li&gt;Default panic message/prefix: &lt;code&gt;assertion `left matches right` failed&lt;/code&gt;&lt;/li&gt;
646+
&lt;li&gt;Non-prelude assertions to replace &lt;code&gt;assert!(matches!(value, pattern))&lt;/code&gt;&lt;/li&gt;
635647
&lt;/ul&gt;
636648
&lt;/li&gt;
637649
&lt;/ul&gt;
638650
&lt;/li&gt;
639-
&lt;li&gt;Rust cleans up memory by destroying variables on the stack in reverse order after a panic occurs. This process is called Unwinding.&lt;/li&gt;
640651
&lt;/ul&gt;
641-
&lt;h2 id="panic"&gt;&lt;code&gt;panic!&lt;/code&gt;&lt;/h2&gt;
642-
&lt;p&gt;Mainly used with unrecoverable errors.&lt;/p&gt;</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>&lt;h2 id="bool"&gt;bool&lt;/h2&gt;
652+
&lt;/li&gt;
653+
&lt;li&gt;
654+
&lt;p&gt;Rust cleans up memory by destroying variables on the stack in reverse order after a panic occurs. This process is called Unwinding.&lt;/p&gt;</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>&lt;h2 id="bool"&gt;bool&lt;/h2&gt;
643655
&lt;p&gt;true or false&lt;/p&gt;
644656

645657

0 commit comments

Comments
 (0)