A trip to the library in Rust

Why write code you don't have to?

Chances are someone's already written it and put a crate up on crates.io. A "crate" in Rust is just another name for a library or package, code that you can use in your own project.

The other day I wanted a randomly generated string of text so I could manually append it to a CSS class name to make (pretty) sure it was unique. Let's write an extremely simple commandline application that does this!

We already know how to print stuff to the console, so all we need is a random string generator library. There's a crate I've used before called nanoid, which might do the trick.

Use your existing Hello World project or create a new one with cargo new.

cargo new rando # or whatever you want to call it

We add the crate using the cargo add command.

cargo add nanoid

This will add the latest version of nanoid to your Cargo.toml file.

[dependencies]
nanoid = "0.4.0"

We can now use the nanoid crate directly in our code, using :: to access the nanoid! macro inside the crate.

// main.rs

fn main() {
    let id = nanoid::nanoid!();
    println!("{}", id);
}

Alternatively, we can use the use keyword to bring the macro into scope. Then we don't have to use the fully qualified name with the double colon syntax :: every time we want to use it.

use nanoid::nanoid;

fn main() {
    let id = nanoid!();
    println!("{}", id);
}

That's our app anyway. Run it with cargo run and we'll get a random string of text printed to the console. But it might be fun to actually install it on our system so we can run it any time we want.

To do this simply run cargo install --path . in the root of your project. This will install the app in ~/.cargo/bin, which should have been added to your PATH when you installed Rust. You can now run it from anywhere on your system.

In my case, I called the app rando so now whenever I want a random string of text printed to the console I can just run rando and hey presto I get that random string of text.

To uninstall the app just run cargo uninstall rando.

That's it for today. Happy coding!


Learn more about crates and packages etc in the Rust book.

What cargo new gives you

Last time we used cargo new to generate a new Rust app. Let's take a look inside.

Run tree to print the directory structure.

.
├── Cargo.lock
├── Cargo.toml
└── src
    └── main.rs

There's also a hidden dotfile .gitignore and the .git folder, but don't worry about those for now.

The Cargo.lock file is generated by Cargo by cargo run and it's for dependency tracking and that's Cargo's problem.

The two important files are Cargo.toml and src/main.rs.

# Cargo.toml

[package]
name = "hello"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

Cargo.toml is our project's manifest, where we keep track of stuff about the project in TOML format and also any dependencies we want to use. Currently there's none!

Name and version are pretty self-explanatory. The edition key is for specifying which edition of Rust we want to use ie. latest stable edition, 2021.

The src/main.rs file is our entry point.

// src/main.rs

fn main() {
    println!("Hello, world!");
}

When compiling, Rust will look for the src/main.rs file with a main function inside. Functions in Rust are defined with the fn keyword.

If Rust finds a src/lib.rs file it will build a "library", which can be used as a dependency inside another app instead of a standalone binary app. (We can also have both apparently. But we'll get to that later.)

To print "Hello, world!" to the console we use the println! macro, which is built-in to Rust. Macros are kinda like functions that expand into other functions at compile time. They have a ! at the end. For now, you just need to know that println!() prints stuff to the console.

Aaaaaand that's just about it. Let's edit some code. We can make the computer say whatever we want!

println!("Crab party time! 🦀🦀🦀🦀🦀🦀🦀🦀🦀");

Do another cargo run and give us a little happy dance.

Nice. Until next time!