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.
Let’s talk about it.
Creating a project
You can use cargo to bootstrap your projects by running:
cargo new project_nameCode 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:
Or you can fetch those dependencies with:
If you are building production, you just need to add
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.rsCode 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:
How about code formatting?
rustfmt. But just like how you do not need to run eslint directly you can run
rustfmt using cargo by typing:
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
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
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:
--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.
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 .