liveBook · Manning

Name mangling

As explained in Refactoring to Rust

Name mangling is the compiler transformation of symbols into unique names that encode additional implementation information.

Name mangling transforms function and type names into unique symbol names by adding implementation information. In Rust, this helps avoid symbol collisions. For C linkage, `#[no_mangle]` disables the transformation so an exported function keeps the name C expects. section 3.2.2

Listing 3.6 Rust solve function that can be exported as compatible with C
#[no_mangle]
pub extern "C" fn solve(
  line: *const c_char, solution: *mut c_int) -> c_int {
  0
}

This Rust `solve` declaration shows `#[no_mangle]` alongside the visibility and calling-convention annotations used for C linkage. section 3.2.2

How Refactoring to Rust covers it

How deep it goes
Defines mangling through symbol uniqueness and collisions, gives a Rust symbol example, and leaves the compiler’s naming method unexplained.
Examples
Uses Rust’s `solve` function and its mangled symbol as examples of an exported function name.
What it assumes
Explains name mangling for readers unfamiliar with C++, while situating it in Rust-to-C linking.
What you can do after
Apply `#[no_mangle]` to keep an exported Rust function’s symbol unmangled for C linkage.
How it connects
Connects Rust symbol names with C linking, public visibility, calling conventions, and Unix-like libraries.

Also explained in

Compare how 3 books explain name mangling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.