What's the right way to do things like terminal mode restoration (i.e., back to sane mode) and cleanup of temporary files (deleting them) in Rust? In particular, would really like this to work correctly even if there's a panic. But even if that's not possible, want to make sure that every "normal" path out of the program does the cleanup.
RAII. Basically, have some type own your resource (temporary file)/transformation (terminal mode), implement Drop on this type, and then clean up your resource inside drop. See the tempdir crate on crates.io for a good example.
Note: Rust will call destructors on panic/return but will not call them on abort (std::intrinsics::abort
)/exit (std::process::exit
).
1 Like