I am making an crossplatform terminal library. Because my library changes the state of the terminal, I need to revert all the changes that are made to the terminal when the process ends. I am now implementing this feature and thinking of ways how to restore to the original terminal state at the end.
I thought that a static variable is initialized when the program starts and that when the program ends this static variable will be destroyed. Since my static variable is a struct which has implemented the Drop
trait, it would be dropped at the end of the program, but this is not the case because the string "drop called" is never printed:
static mut SOME_STATIC_VARIABLE: SomeStruct = SomeStruct { some_value: None };
struct SomeStruct {
pub some_value: Option<i32>,
}
impl Drop for SomeStruct {
fn drop(&mut self) {
println!("drop called");
}
}
Why is drop()
not called when the program ends? Are my thoughts wrong and should I do this another way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…