The Cargo package manager is basically npm for Rust. It’s used for builds and dependency management and was installed along with Rust. You also use it to run your linting, tests, and even the program itself. I wasn’t kidding about that npm comparison.
Its usage is so similar to npm that if you are a JavaScript dev, you will have a good first grasp after 5 minutes. It’s an important part of Rust development and is worth a post.
Let’s talk about it.
You can use cargo to bootstrap your projects by running:
cargo new project_name
Code language: Bash (bash)
This will create a new project named project_name and initialize a git repo for you.
Cargo uses a weird config format because apparently, YAML is not good enough for it. The config format is called TOML; this was my first introduction to it. It’s an interesting and pretty easy format to understand. You can also find the rust manifest format here which shows you the options for your rust project.
To my joy, rust expects source files in the src folder, yay. I like that file structure, it makes more sense than having your source files and config all in the root directory.
To build your app you use:
cargo build
When you clone a rust repo, just like in Javascript you won’t have all the dependencies installed, so you will need to run build to get those dependencies.
Or you can fetch those dependencies with:
cargo fetch
If you are building production, you just need to add --release
cargo build --release
You have to compile your application in order to run it. So if you want to do it manually you’d need to compile
rustc filename.rs
Code language: CSS (css)
Which creates an executable file that you can call. But there is an easier way. To run your app the easy way, you use:
cargo run
We all like pretty code, or at least code that’s consistent. In JavaScript, I use eslint with Standard.js. In rust to lint your code you have to run rustfmt
. But just like how you do not need to run eslint directly you can run rustfmt
using cargo by typing:
cargo check
On a side note, rust likes snake_case for its project names. Good to know, annoying but good to know.
If there are linting errors you can automatically fix them, if they are fixable with
cargo fix
If you don’t want to write everything from scratch, you’ll need crates. These are modules that provide some functionality to your application. For example regex, HTTP, google etc. You can add a new crate by using:
cargo add dependency
Or you can add it to the config toml and call cargo fetch
.
Removing is pretty straightforward as well, just remove the package from the dependencies list in cargo.toml then fetch or build again.
Rust is not a rogue language that abhors testing. You can write and run tests pretty easily and to run those tests you can call:
cargo test
Just like JavaScript, you can run your cargo commands in your favorite CI tool, and what could be useful is the --verbose
option that works on test, build, etc.
Using cargo also introduces conventions about folder structure. You can read more about those here.
The cargo book is extensive so I just touched on what I thought would be important for getting started and what I needed to remember.
Honestly, cargo is pretty much my JavaScript workflow with the word cargo instead of npm or yarn. And like yarn and npm we even have a cargo.lock
file.
Seriously, it’s the same. Great news for me since that’s less I have to learn. There is way more I could say about cargo and even more I do not yet know but I don’t know everything about npm or yarn. This should be enough to get me started.
If you have other cool tips or trips or even bits of general knowledge about cargo you’d like to share hit me up on Twitter, @phoexer, and as always happy coding.
Image Credits: Cargo-Logo-Small by Cargo Docs used under .
Today I am working on a summation problem made to look like building a tower out of cubic bricks. It’s fun to brute force sometimes.
Coming back to Rust code after a bit of a hiatus with a simple problem… The Two-sum problem. Finding numbers in an array. Yay, fun.
King Pinn: I Salute You. I’m driving halfway across the country and this song is on repeat. Rest in Peace Tonderai Makoni. You were awesome.
After a few weeks off I’m back to business. This is just an update post detailing plans for the rest of the year.
At last we finally have the great reveal, our mystery project was implementing RSA encryption in rust.