When is `std :: process :: exit` OK to use?

The documentation std::process::exitreads:

If a clean shutdown is required, it is recommended that you only call this function at a known point where there are no more destructors left to run.

Perhaps due to the lack of a system programming background, I don't mean if there are destructors left to run at a certain point, and if I take care. The only thing that comes to my mind is waiting for write operations to a file (or something else), where it’s nice to leave things in a clean state.

What else do you need to follow? I suspect that this is not recommended for use in larger, more complex programs, but for small tools it seems convenient.

+4
source share
1 answer

Short answer:

  • most likely use in panic!()almost all cases
  • you can pretty much make sure no destructors are left if ...
    • ... you are in function main()
    • ... you manually process the stack, including unwinding (you probably don't ...)
  • sometimes you can just ignore the destructors when you exit the program anyway, but you need to be careful!

A little longer explanation

[...] for small tools seems convenient.

If you want to exit your program due to a fatal error, we recommend using panic!(). This will spin up the stack (start all destructors) and exit the program with additional information (including the message line that you can specify).

[...], , , .

, , , Drop . - , ( ). , , Drop.

, - , (" main() ). , main().


Drop? , . i32: , , ( ). , Drop. :

  • : Box<T>, Vec<T>, HashMap<T>,...
  • : File, Socket,...
  • : Ref, MutexGuard,...
  • ...

, . , , : . , . : .

. : , Drop, . , . , .

+6

Source: https://habr.com/ru/post/1653003/


All Articles