Difference between Rust filename extension .rs and .rlib

while reading wikipedia i notice two filename extension .rs and .rlib for Rust-lang .i write a hello-world in both extension, both are working properly. Is there any difference between this two file extensions .In case .rlib is used to represent rust library then why cargo new --lib
create lib file with .rs extension instead of .rlib.
is there any additional advantage for writing rust library with .rlib extension

You can compile your lib crate into .rlib files so it can be used by other crates. Normally this process is done automatically by cargo so you will not see any .rlib files.

The two aren't interchangeable. The .rs extension is for source codes and .rlib is for compiled artefacts... It's kinda like the difference between .c and .dll.


can any one explain more in detail and applications of .rlib

You may find this section of the reference helpful. It explains the different crate types the compiler can output, including rlib.

The rlib files are basically just static libraries with additional metadata for the compiler to understand them. I think they are mainly used as intermediate stages in the compilation of a binary - dependencies are compiled to rlib so they can be linked into another library or executable and so they don't need to be recompiled every time. You can in theory also pass rlib files around separately and pass them to rustc, but I'm not sure if the format has stability guarantees. A static library for linking into a non-Rust program can be generated with the staticlib crate type.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.