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. It helps the programmer to avoid extra work by not having to implement any logic or program again and again and a library in Rust is also called a crate which are of two types namely Binary crate and Library crate where Binary crate creates the entry point which is provided by cargo by default by creating a Crago. toml file and Library crate is when the root of the crate is src/lib.rs contained in the package.
The syntax to declare library in Rust is as follows:
There is no syntax as such to declare or define library in Rust.
Working of library in Rust is as follows:
- 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.
- A Library in Rust helps the programmer to avoid extra work by not having to implement any logic or program again and again.
- A library in Rust is also called as 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 are of two types namely Binary crate and Library crate.
- When the command cargo new project_name is entered, a Cargo. toml file is created which is nothing but the Binary crate is 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.
- There are four steps involved in the construction of a library crate.
- The four steps involved in the construction of the library crate are project setup, writing code or functionality, obtaining the credentials from the crate registry, and publishing a library.
- Setting up the project is the first step in building a library in any programming language.
- The meaning of setting up a project means what the user is aspiring to build.
- We have to make use of the command new –lib project name to set up our project as a library.
- Once the command is executed, a library will be set up consisting of src package and Cargo. toml file.
- rs is present in 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 are going to 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 the code or functionality is written, the construction of library is done and the next step is to make the library public so that it is available to use for other users as well.
- A library can be made global or public by making use of a Crate Registry is Rust 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.
- The access token to access the Crates Registry can be obtained from the Application Programming interface access under the Account Setting.
- Once the library is built and made public so that it is available to all the users, the next step is to publish the library.
- The library can be published in the Registry by logging into the Registry using cargo login access token.
- Once the login to the cargo login access token is successful, cargo publish must be run from the directory of the project to publish the library in Crates, io Registry.
- Crate std is the Rust standard library which is the foundation of the Rust software and makes its 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.
Conclusion
In this article, we have learnt the concept of library in Rust-through definition, types of libraries, and steps involved in the construction of libraries, and the data types, modules supported by Rust Standard Library.
Recommended Articles
This is a guide to Rust Library. Here we discuss the introduction, syntax, Working of library in Rust, and concept of library in Rust-through definition. You may also have a look at the following articles to learn more –
41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses