Remix.run Logo
ameliaquining 3 hours ago

The relevance to Rust is precisely that it doesn't have life before main at the language level; therefore, if you need it*, you need to use these kinds of linker hacks (which fortunately are amenable to encapsulation through macros). By contrast, if the article were about C++, the focus would be on "what happens under the hood when you use static initialization, in case you were curious" rather than "how to use these low-level mechanisms to do something not otherwise possible".

* Which you should think very carefully before concluding is the case, as it's responsible for rather a lot of bugs in C++. I think in Rust it is mostly used for registry-pattern type stuff since the const system can't currently(?) handle that.

Animats 12 minutes ago | parent | next [-]

Yes. There isn't Rust language support for this.

Order of initialization can be supported at various levels:

- Completely random (OK if interdependence are locked out, otherwise bad) - Consistent, but sorted by something such as alphabetical name (meh.) - Manual, controlled in linker scripts (headache) - True dependency tree order, including diagnosing loops (seen in the Modula family).

General comment: yes, you can, and you probably shouldn't unless you have profiling data that indicates a significant performance improvement for a critical use case.

kibwen 2 hours ago | parent | prev [-]

[dead]