How it works...

In this recipe, we created a sample library and made a .rlib extension package of the code. This will help us call the units of the library in other crates or Rust programs.

We created two public functions named public_function and indirect_access and a private function named private_function. We called private_function from indirect_access, which is a way through which we can call it outside the scope. Then, we created the library using the Rust compiler and passed a few command-line arguments to it, which tells the compiler to create the library format of the script.

While compiling the program, we ran rustc --crate-type=lib sample_lib.rs, which created a package named libsample_lib.rlib in the same directory. This file can be externally used at other crates. Alternatively, we can use Cargo to ship libraries by adding the [lib] tag in the Cargo.toml file.

Usually, libraries get prefixed with lib, and by default, they get the name of their crate file. But this default name can be overridden using the crate_name attribute while creating the library using rustc.
..................Content has been hidden....................

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