Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equivalent to Python atexit functionality #712

Open
steveklabnik opened this issue Jan 23, 2015 · 3 comments
Open

Equivalent to Python atexit functionality #712

steveklabnik opened this issue Jan 23, 2015 · 3 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@steveklabnik
Copy link
Member

Issue by bmurphy1976
Tuesday Jan 21, 2014 at 01:54 GMT

For earlier discussion, see rust-lang/rust#11695

This issue was labelled with: in the Rust repository


I can't always guarantee that users are going to properly use a library (calling init methods, cleanup methods, etc.).

Occasionally things die horribly and you want to provide stronger guarantees about when things fail.

Is there a way to hook up to an exit signal of sorts to execute code when a program exits regardless of the exit mechanism? Something like Python atexit?

@sfackler
Copy link
Member

This currently exists as std::rt::at_exit: http://doc.rust-lang.org/std/rt/fn.at_exit.html

Though I believe the callbacks currently don't trigger due to some problems from runtime removal and I'm not sure if it'll be stable for 1.0.

@petrochenkov petrochenkov added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Jan 20, 2018
@jrop
Copy link

jrop commented Sep 11, 2019

@steveklabnik The link above to std::rt::at_exit is dead. Is there a modern alternative currently in existence for Rust 2018 edition? A colleague and I have run across the same need: we need to run some cleanup code when our (internal) library is not in use.

@RustyYato
Copy link

RustyYato commented Sep 11, 2019

@jrop A quick search on crates.io shows

But these are old, a more modern solution is ctor, you can use the #[dtor] attribute to hook into shutdown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

5 participants