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:
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
loop {}
// on std targets
use std::{thread, time};
let ten_millis = time::Duration::from_millis(10);
thread::sleep(ten_millis);