blob: fea49a74d04e0cfebe15598e7848293327f02527 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
### What it does
Checks for empty `loop` expressions.
### Why is this bad?
These busy loops burn CPU cycles without doing
anything. It is _almost always_ a better idea to `panic!` than to have
a busy loop.
If panicking isn't possible, think of the environment and either:
- block on something
- sleep the thread for some microseconds
- yield or pause the thread
For `std` targets, this can be done with
[`std::thread::sleep`](https://doc.rust-lang.org/std/thread/fn.sleep.html)
or [`std::thread::yield_now`](https://doc.rust-lang.org/std/thread/fn.yield_now.html).
For `no_std` targets, doing this is more complicated, especially because
`#[panic_handler]`s can't panic. To stop/pause the thread, you will
probably need to invoke some target-specific intrinsic. Examples include:
- [`x86_64::instructions::hlt`](https://docs.rs/x86_64/0.12.2/x86_64/instructions/fn.hlt.html)
- [`cortex_m::asm::wfi`](https://docs.rs/cortex-m/0.6.3/cortex_m/asm/fn.wfi.html)
### Example
```
loop {}
```
|