+Calling functions and methods in the Rust std
library from a #[ctor]
or #[dtor]
function is not safe. This is because the std
library only guarantees stability and portability between the beginning and the end of main
, whereas #[ctor]
functions are called before main
, and #[dtor]
functions are called after it.
+
+Do not call any part of the std
library from a #[ctor]
or #[dtor]
function. Instead either:
+
main
function.
+In the following example, a #[ctor]
function uses the println!
macro which calls std
library functions. This may cause unexpected behavior at runtime.
+
+The issue can be fixed by replacing println!
with something that does not rely on the std
library. In the fixed code below, we used the libc_println!
macro from the libc-print
library:
+