title: How to add a shutdown hook to a Rust program?
body: <p>I'm writing a Rust program which needs to save some data at the end of its execution, whatever happens.</p>
  
  <p>In the Java world, I would do that with a <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)" rel="nofollow noreferrer">shutdown hook</a>. There is a crate, aptly called <a href="https://docs.rs/shutdown_hooks/" rel="nofollow noreferrer">shutdown_hooks</a>, but it seems only able to register <code>extern "C"</code> functions (and I'm not totally sure it will run on <code>panic!(...)</code>).</p>
  
  <p>How can I implement a shutdown hook that triggers on normal exit as well on panic?</p>