By DeepSource

Found empty loop expression RS-P1000


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 or std::thread::yield_now.

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
  • cortex_m::asm::wfi

Bad practice

loop {}


// on std targets
use std::{thread, time};

let ten_millis = time::Duration::from_millis(10);