rust

When was no_mangle required to be wrapped in unsafe()?


This won't compile on my compiler, rustc 1.86.0:

#[no_mangle]
fn function_in_rust(arg1: i32, arg2: i32) -> i32 
{ 
    arg1 + arg2
    
}

And I have to do:

#[unsafe(no_mangle)]
fn function_in_rust(arg1: i32, arg2: i32) -> i32 
{ 
    arg1 + arg2
    
}

However on some other Rust compilers it works. When did this happen exactly? With which version? And doesn't this break all previous code?


Solution

  • When did this happen exactly? With which version?

    Not a version, an edition, specifically edition 2024: https://doc.rust-lang.org/edition-guide/rust-2024/unsafe-attributes.html

    That edition became available with rust 1.85, but using rust 1.85 or later does not require using edition 2024.

    And doesn't this break all previous code?

    Because it's an edition change, no: an older codebase has to opt into edition 2024 semantics (via cargo.toml, or a rustc flag) even if it updates the toolchains to the latest.

    Plus cargo fix can generally handle most edition changes.