196 words
1 minutes
no_mangle
  • The #[no_mangle] attribute in Rust tells the compiler not to change (mangle) the name of a function or variable in the compiled binary.

What is Name Mangling?#

  • Normally, the Rust compiler transforms your original function names into an encoded string (e.g., adding module paths, crate names, and type signatures) so the linker can uniquely identify them.

Why Use #[no_mangle]?#

Important Notes#

  • • Safety & Unsafe Attributes: Because disabling name mangling can lead to symbol name collisions and undefined behavior if two different libraries use the same name, modern Rust (Edition 2024 / Rust 1.82+) requires writing it as #[unsafe(no_mangle)] to acknowledge these safety risks.
  • • Usage Example:
#[unsafe(no_mangle)]
pub extern "C" fn hello_from_rust() {
    println!("Hello from Rust!");
}
  • Are you trying to call Rust from C/C++, export a function to WebAssembly, or fix a compiler warning about unsafe attributes?
no_mangle
https://younghakim7.github.io/blog/posts/no_mangle/
Author
YoungHa
Published at
2026-10-15