Category Archives: Cargo

Cargo

As part of a little project I’m working on, I’m back playing within Rust.

Whilst we can use rustc to build our code we’re more likely to use the build and package management application cargo. Let’s take a look at the core features of using cargo.

What version are you running?

To find the version of cargo, simply type

cargo --version

Creating a new project

Cargo can be used to create a minimal project which will include the .toml configuration file and the code will be written to a src folder. Cargo expects a specific layout of configuration and source as well as writing the building artifacts to the target folder.

To generate a minimal application run the following (replacing app_name with your application name)

cargo new app_name

We can also use cargo to create a minimal library using the –lib switch

cargo new lib_name --lib

Building our project

To build the project artifacts, run the following from the root of your application/library

cargo build

This command will create the binaries in the target folder. This is (by default) a debug build, to create a release build ass the –release switch i.e.

cargo build --release

Running our project

In the scenarios where we’ve generated an executable, then we can run the application by cd-ing into the target folder and running the .exe or via cargo we use

cargo run

Check your build

In some cases, where you are not really interested in generating an executable (for example) you can run the check command which will simply verify your code is valid – this will likely be faster than generating the executable and is useful where you just want to ensure your code will build

cargo check