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