Similar to Perl-CPAN, R-CRAN, Python-Pypi, Java-Maven, Rust has crates.io, where one can publish the code for others to utilize the functionality. In this post, let’s explore,

  1. Using one such publicly available crate, rand.
  2. Rust Modules.
  3. Rust Crates.
  4. Publishing our own crate, movies.

Let’s get started!

rust-logo

  • Using rand crate.

    If you want to follow along and bring up Rust environment locally, please feel free to clone my github repo and compose up using,

git clone https://github.com/vangalamaheshh/rust-esh-cean
cd rust-esh-cean
docker network create rust-net
docker-compose up -d
docker exec -it rust-esh-cean bash

Now you are on Rust docker container and ready to code along …

cargo new movie-crate-demo
cd movie-crate-demo

Time to add rand crate to project dependency list.

Add the following to Cargo.toml file.

//...

[dependencies]
rand = "0.8.5"

Running cargo run will fetch rand crate as showed in the output below.

Updating crates.io index
  Downloaded cfg-if v1.0.0
  Downloaded rand v0.8.5
...
Running `target/debug/movie-crate-demo`
Hello, world!

Add the following to src/main.rs file.

use rand;

fn main() {
    let mut counter: i8 = 0;
    while counter < 10 {
        counter += 1;
        println!("{}) {:+}", counter, rand::random::<i8>());
    }
}
Output:
1) +18
2) -33
3) -28
...
9) -74
10) -109
  • Rust Modules.

Let’s create a module movies and add a function play that prints a movie name that we pass to this function.

Create a file src/movie.rs with following content.

pub mod movies {
    pub fn play(name: String) -> String {
        format!("Playing movie: {}", name)
    }
}

It’s time to include this module in our Cargo.toml.

[lib]
name = "movies"
path = "src/movies.rs"

Now, let’s test our movies module by adding following code to our src/main.rs file.

use movies as movie_module;

fn main() {
    let name = String::from("Good Will Hunting");
    println!("{}", movie_module::movies::play(name));
}
Output:
Playing movie: Good Will Hunting
  • Rust Crates.

From within the folder, movie-crate-demo, run the following to create movies crate and move exising movies module into movies crate.

cargo new --lib movies
mv src/movies.rs movies/src/

Remove the following content from movie-crate-demo/Cargo.toml and in turn add this content to movie-crate-demo/movies/Cargo.toml.

[lib]
name = "movies"
path = "src/movies.rs"

Add the following to movie-crate-demo/Cargo.toml file.

[dependencies]
movies = { path = "movies" }

Edit movie-crate-demo/src/main.rs file with the following,

extern crate movies as movies_crate;
use movies_crate::movies;

fn main() {
    let name = String::from("Good Will Hunting");
    println!("{}", movies::play(name));
}
Output:
Playing movie: Good Will Hunting
  • Publishing Movies Crate to crates.io

In order to publish crates, you need to create an account on crates.io. Run the following to publish our movies crate to crates.io.

cd movies
cargo login <api-token>
cargo package --allow-dirty
cargo publish --allow-dirty

That’s it. You now have your movies crate published onto crates.io. Here’s how it looks after I published my movies crate.

movie-crate

  • Using our published Movies Crate in our project.

    Edit the movie-crate-demo/Cargo.toml as follows,

[dependencies]
movies = "0.1.0"

Running cargo run will download movies crate from crates.io and will output,

Playing movie: Good Will Hunting

To recap, we explored,

  1. Using rand crate.
  2. Created movies module.
  3. Created movies crate.
  4. Published movies crate to crates.io.
  5. Used movies crate in our project.

Happy Publishing Rust Crates!! :+1:

Buy Me A Coffee