What Is the Difference Between staticlib and cdylib in Rust?

staticlib creates a static archive for direct linking, while cdylib creates a dynamic shared library with a C-compatible ABI for external use.

Use staticlib to create a static archive (.a or .lib) linked directly into the final binary, and cdylib to create a dynamic shared library (.so, .dll, .dylib) with a C-compatible ABI for external languages.

[lib]
name = "my_library"
crate-type = ["staticlib", "cdylib"]
#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
    a + b
}
# Build static library
cargo build --lib --release
# Output: target/release/libmy_library.a

# Build dynamic library
cargo build --lib --release
# Output: target/release/libmy_library.so