Calling Rust from another language

Rust can also be called from a different language and it's a simple process. The only caveat is that the name used has to be unmangled. If you recall from Chapter 8, The Rust Application Lifetime, when you use a generic, the compiler generates the necessary code to ensure the linker works. It does this by mangling the names to ensure that the correct code is compiled and called when the code needs it.

Unmangling is the opposite of this; it preserves the name of the function in use:

#[no_mangle] 
pub extern fn hello_world() -> *const u8 { 
    "Hello, world!".as_ptr() 
} 

This can then be called from within your own (non-Rust) application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset