Get the hang of Constructing a Library in Rust

Reading Time: 4 minutes

According to the definitions, “A Library is a collection of precompiled, reusable programs, routines, or scripts that a programmer can call when writing code“. Libraries help a programmer not to re-implement any program or logic again.

In this article, you’ll get to know how to write a Library (or you can say a Crate) in Rust Programming Language.

Before proceeding with our major concept i.e, Constructing a Library/Crate. Let’s understand the types of crates in Rust Programming we have.
There are two types of Crates we have in Rust:

  • Binary Crate
  • Library Crate

Binary Crate

Binary Crate is provided by cargo by default, when we enter the command cargo new project_name then cargo creates a Cargo.toml file which follows a convention that src/main.rs is the crate root binary crate with the same name as the package. In short, when we have src/main.rs so it is binary crate because it is the entry point of our binary.

Library Crate

A crate turns into to Library Crate when a package contains src/lib.rs as a crate’s root. Because Cargo knows that if the package directory contains src/lib.rs, the package contains a library crate with the same name as the package, and src/lib.rs is its crate root.

Now, let’s understand how to create a Library Crate because this article is all about the Construction of Library Crate.
To create a library we have to follow some steps:

  • Project Setup
  • Write Code(Functionality)
  • Getting Credentials from Crate Registry
  • Publish a Library

These are the major steps to construct a library let’s understand these steps one by one:

Project Setup

This is the first setup towards building a library not only for Rust but for any programming language.
Setting up the project defines what type of crate a user wants to construct. Though there are two ways present in Rust Language one for Binary and one for Library. But as of now, we’ll go with a Library one.
So, to construct a library we have to set up our project as a library by using this command.
cargo new --lib project name
This command will set up the project as a library that contains an src package and a Cargo.toml file. And src package contains lib.rs because we are constructing a library crate.
Like:

Write Code (Functionality)

So this would be our second step to construct a Library Crate. Here we have to write code for which we want to create a library. This is the major part of our library where our precompiled programs, scripts, or routines reside. And you have to write your code in lib.rs or you can add modules as well. You’ll get the lib.rs in the project itself. Like:

Getting Credentials from Crate Registry

Now you’re done with your library construction and you can easily use it in your local machine as like as other libraries.
So, to make it available for other users also you all have to make your library public. So that other users can also use it.
The process of making a library public or global, Rust provides a Crate Registry called Crates.io.
To get access in this registry you have to follow some steps:

  • You have to log in with your GitHub account.
  • After a successful login, you need to verify your email address.
  • Now the last step is to get the access token, and you can get it from API Access in the Account Setting.

Now you have your access token through which you can login to the Crate Registry.

Publish a Library

This is the last step of this article, where you’ll make your created Library open to all or global. This Crate Registry is the platform through which you can make your Library global. So, to publish that library into Registry, you have to login first through cargo login access_token and after successful login, the last step is you have to run cargo publish from the project’s directory. And you will see that your library is published in the Crates.io registry.

This is all about for this article.
Thanks for reading!!!


Knoldus-blog-footer-image

Written by 

Pawan Singh Bisht is the Trainee Software Consultant at Knoldus Software LLP. He has good knowledge of languages C++ and Java, and currently working on Rust. He is always ready to accept new challenges. As a fresher, he always tries to explore the different type of software and tools.

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!