Skip to content
Related Articles

Related Articles

Rust – Creating a Library

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 24 Apr, 2022

Rust is a multi-paradigm programming language like C++ syntax that was designed for performance and safety, especially for safe concurrency. Also, it is a compiled system programming language. In this article, we will see how to create libraries in Rust.

Creating a Rust Library:

Step 1: We start by creating a .rs file called rary.rs. This file contains the contents of our library.

Example:

Rust




// Name of the file = rary.rs
pub fn public_function() {
    println!("Hello I am a public function");
}
  
fn private_function() {
    println!("Hello I am a private function");
}
  
pub fn indirect_access() {
    print!("Accessing a private library using a public function");
  
    private_function();
}
  
fn main(){
    public_function();
    indirect_access();
 }  

Output:

 

Step 2: Now we will create this library using the following command.

$ rustc --crate-type=lib rary.rs

Step 3: The above command will generate file “library.rlib” . All libraries get prefixed with “lib” and by default, they are named after their crate file. So “library.rlib” –> lib + rary + .rlib

Step 4: The following command can override the default name.

$ rustc --crate-type=lib rary.rs --crate-name "<Name of your choice>"

So, by using the above steps you can create our own Rust library.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!