How to Create and Publish a Rust Library
Rust is a new programming language that has an ability to manage memory and provide more security than other languages. We can compare this language with other low level languages like C and C++ that has more control over the low level architecture of computer resources. It is mostly used in ‘System Programming’ such as operating systems and compiler design etc.
In order to create a library in rust, we must have essentials utilities to create one. So, first we need to install rust in our System.
Install Rust
- Go to Getting started — Rust Programming Language (rust-lang.org)
- Download executable(.exe) that matches your OS type (32-bit/64-bit)
- To run rust, you must have C++ build tools. By double clicking on the exe file, you might ended up with the below screen:
- Type ‘1’ and <Enter> to ‘Quick Install via the Visual Studio Community installer’ which is free for individuals, academic uses, and Open Source!. It will downloads the ‘visual studio installer’
- Then, you may ended up with the following screen, select ‘MSVC v143 — VS 2022 C++ x64/x86 build tools (Latest)’ and click on ‘Install’
It will download and install the build tools, that will be required to run rust programs. After install, it may automatically open ‘Microsoft Visual Studio’. We don’t want it, close it. Now, we done the setup of C++ build tools. Then come to ‘.exe’ file, open the file. You may see the screen like below:
- Type ‘y’ and press <Enter>
- select ‘1’ to ‘Proceed with installation’
- It will install some components like cargo (Build System in rust), clippy, rust-docs, rust-std (Standard libraries in rust), rustc (compiler), rustfmt.
- Close the ‘Terminal’ and open it again (Restart the terminal)
- To check whether everything is installed correctly. Type the following commands. It will return the version.
rustc --version
cargo --version
rustup --version
That’s all we setted up the environment to create a rust project/library.
Create a Rust Library
- open ‘Command prompt’ or ‘Terminal’ and type the following command to create a rust library
cargo init rustLib --lib
- Here, we created a new rust package by using library template( — lib)
- Change directory to the library (cd rustLib) and open the current directory in your terminal by typing ‘code .’ in the terminal
- Now, you can see one directory (src = This is where our rust code lives)and two files (.gitignore (text file that instruct git to ignore certain files or folders), Cargo.toml (configurations for the project))
- Open src -> lib.rs, write the following sample function (Delete default code present inside that file)
fn rust_function(){
println!("I am a rust library function");
}
Publish the library to crates.io
crates.io : It is a package registry for rust where you can go to publish your library or download and use others libraries.
- Try to run the publish command (Try-1):
cargo publish
It will return an error something like this:
To publish a library, we need an account in crates.io and a token.
- Go to crates.io: Rust Package Registry
- Click on ‘Login with Github’
- Then, go to ‘Account Settings’ -> API tokens and click on ‘New Token’ and type a name for that token.
- It will generate a token. Make sure to copy and paste it somewhere. Because, it will be not show to you in future.
- Back to Visual studio and type the command in terminal to login to our account:
cargo login
- This command will ask you the token. paste the token here and <Enter>
- ‘Login token for crates.io saved’
- Try to run the publish command (Try-2):
cargo publish
Again. Some error message!
It says, we must configure our library and must commit it into git (otherwise you can proceed this and include the uncommitted changes, pass the — allow-dirty flag [Avoid using this flag, this can cause loss of library files])
- Create a Github repository for this library (Note down the repo name)
- Change the Cargo.toml file with the following attributes:
[package]
name = "rustLib"
version = "0.1.0"
authors = ["Karthik Rathinavel"]
description = "Sample library in rust"
license = "MIT OR Apache-2.0"
homepage = "https://github.com/karthikrathinavel/rustlibrary1"
repository = "https://github.com/karthikrathinavel/rustlibrary1"
edition = "2021"
readme = "README.md"
[dependencies]
- Add a README.md file with some description of your library
- Migrate or upload the library (/src/lib.rs and Cargo.toml and README.md)into your github repository.
- Try to run the publish command (Try-3):
- Wait… We need to commit this into our local git.
- Type the couple of following command in your terminal (Before writing those commands you need to install git bash and set the environmental variable for it):
git add .
git commit
- git add . = Add the files from untracked state to added state
- git commit = save those files into git as a commit.
- Then run the publish command:
cargo publish
Now, the library can be published to crates. If you still encounter the same error, you will try restarting visual studio and try again ‘cargo publish’.
It shows some warning about the naming convention and dead code. If you want to be perfect, correct this. That’s all, our library is published into crates.io
Now, go to crates.io to check whether our library is published or not.
Here is our library!!! To use the library into your rust project, you need to simply install the library by this command ‘cargo install rustLib@0.1.0’ and add it in the dependencies section of Cargo.toml just like below:
[dependencies]
rustLib = "0.1.0"
THANK YOU