Updated June 2, 2023
Introduction to Rust Library
A collection of reusable, precompiled programs, scripts, or routines that can be called by the programmer while writing a code is called a Library in Rust. In Rust, a library is referred to as a crate and can be of two types: Binary crate and Library crate. The Binary crate automatically creates an entry point using the default Cargo. toml file provided by Cargo. On the other hand, a Library crate has its root located in src/lib.rs within the package.
The syntax to declare a library in Rust is as follows:
There is no syntax as such to declare or define a library in Rust.
Working of library in Rust is as follows:
- A Library in Rust helps the programmer avoid extra work by not having to implement any logic or program again and again.
- A library in Rust is also called a crate.
- The prefix for a library is lib, and they are named after the crate by default.
- By using the attribute crate name or by passing crate name option to the Rust compiler, the default name for a library can be overridden.
- A crate or library is of two types, namely, a Binary crate and a Library crate.
- When you use the command “cargo new project_name”, Cargo creates a Cargo. toml file, which essentially represents the Binary crate provided by Cargo.
- The root of the Binary crate is src/main.rs, and it is the entry point of the Binary crate.
- When the root of the crate is src/lib.rs, then it is a library crate.
- To construct a library crate, you need to follow four steps.
- To create a library crate, set up the project, write the code, acquire registry credentials, and publish the library.
- Setting up the project is the first step in building a library in any programming language.
- The meaning of setting up a project is what the user is aspiring to build.
- We have to use the command new –lib project name to set up our project as a library.
- Once you execute the command, the library will include the src package and Cargo. toml file.
- rs is present in the src package as we are building a library crate.
- The second step to build a crate is to write the code or functionality in which we will write the code to create a library.
- The precompiled scripts, programs, and routines reside in this code or functionality, and thus this is the prime part of our library.
- The code must be written in lib.rs, and modules can be added as well.
- Once you have written the code or implemented the functionality, you must complete the library’s construction. Then, you can make it public so other users can access and utilize it.
- To make a library globally or publicly accessible in Rust, you can utilize the Crate Registry called Crates.io.
- Access to the Crates Registry requires following certain steps.
- The first step to access the Crates Registry is to log in to the GitHub account.
- Once the log in to the GitHub account is successful, our email address must be verified.
- The final step in accessing the Crates Registry is to get the access token.
- To access the Crates Registry, you can obtain an access token by accessing the Application Programming Interface (API) located in your Account Settings.
- Once you have constructed the library and ensured its accessibility to all users, the next step is to publish it.
- To log in and publish the library on the Registry, you must use your cargo login access token.
- After successfully logging in to the Cargo login access token, you need to run “cargo publish” from the project directory in order to publish the library to the Crates.io Registry.
- Crate std is the Rust standard library, which is the Rust software’s foundation and makes it portable.
- The primitive data types supported by Rust standard library are never, array, char, bool, f64, f32, fn, i128, i64, i32, i16, i8, pointer, isize, slice, reference, tuple, str, u128, u64, u32, u16, u8, usize, unit, etc.
- The modules supported by Rut standard library are u128, u64, u32, u16, u8, usize, vec, slice, string, str, time, thread, sync, task, result, rc, ptr, process, primitive, pin, prelude, path, panic, os, option, etc.
In this article, we have learned the library concept in Rust-through definition, types of libraries, steps involved in constructing libraries, and the data types and modules supported by Rust Standard Library.
We hope that this EDUCBA information on “Rust Library” was beneficial to you. You can view EDUCBA’s recommended articles for more information.