Cargo is the build tool for the Rust programming language. It downloads, compiles and tests your project. More importantly, Cargo manages your project dependencies. Cargo has very good documentation and is very accessible. Cargo het been written in Rust and will be the most important tool in your Rust toolchain! Lets take a closer look.
Installing Cargo
Cargo is automatically installed with Rust. On a Mac, Rust can be installed by typing:
$ brew install rust
$ cargo --version
cargo 1.29.0 (524a578d7 2018-08-05)
Common Commands
Cargo provides commands what you would expect from a build tool like ‘update’, ‘test’, ‘build’ and ‘run’.
$ cargo
USAGE:
cargo [OPTIONS] [SUBCOMMAND]
build Compile the current project
check Analyze the current project and report errors, but don't build object files
clean Remove the target directory
doc Build this project's and its dependencies' documentation
new Create a new cargo project
init Create a new cargo project in an existing directory
run Build and execute src/main.rs
test Run the tests
bench Run the benchmarks
update Update dependencies listed in Cargo.lock
search Search registry for crates
publish Package and upload this project to the registry
install Install a Rust binary
uninstall Uninstall a Rust binary
Basic Workflow
Cargo has a basic workflow like creating a new project, testing code, running the project, and building the project to a static binary.
$ pwd
~/projects
# creating a new project
$ cargo new hello-rust
Created binary (application) `hello-rust` project
cd hello-rust
# testing
$ cargo test
Compiling hello-rust v0.1.0 (file:///Users/dennis/projects/hello-rust)
Finished dev [unoptimized + debuginfo] target(s) in 1.15s
Running target/debug/deps/hello_rust-0b720b8081218362
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
# running
$ cargo run
Compiling hello-rust v0.1.0 (file:///Users/dennis/projects/hello-rust)
Finished dev [unoptimized + debuginfo] target(s) in 2.47s
Running `target/debug/hello-rust`
Hello, world!
# building
$ cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.37s
# runnning
$ ./target/debug/hello-rust
Hello, world!
Cargo.toml
Cargo.toml describes your project and the dependencies. The file is edited by us, the developer. To add dependencies to the project, add new entries in the package
section.
[package]
name = "hello-rust"
version = "0.1.0"
authors = ["dennis <dennisvriend@binx.io>"]
[dependencies]
Dependency Management
Cargo provides crates.io the central package registry that serves as a location to discover and download packages. Cargo is configured to use it by default to find requested packages.
Lets add the rusoto dependency to out project. Rusoto is the AWS SDK for Rust.
[package]
name = "hello-rust"
version = "0.1.0"
authors = ["dennis <dennisvriend@binx.io>"]
[dependencies]
rusoto_core = "0.35.0"
rusoto_s3 = "0.35.0"
Listing the buckets in our account
Lets edit the file src/main.rs
:
extern crate rusoto_core;
extern crate rusoto_s3;
use rusoto_core::Region;
use rusoto_s3::{S3Client, S3};
fn main() {
let client = S3Client::new(Region::EuWest1);
match client.list_buckets().sync() {
Ok(output) => {
match output.buckets {
Some(s3_bucket_lists) => {
println!("Buckets:");
for bucket in s3_bucket_lists {
println!("Name: {}, CreationDate: {}", bucket.name.unwrap_or_default(), bucket.creation_date.unwrap_or_default());
}
}
None => println!("No buckets in account!"),
}
}
Err(error) => {
println!("Error: {:?}", error);
}
}
}
Conclusion
Cargo is the build and dependency management tool for the Rust programming language. Cargo provides build, test and dependency management functionalities for Rust projects. Cargo uses crates.io the central package registry. Cargo uses the Cargo.toml
file to model the project and the dependencies. Cargo is very easy to use and when you have used Maven, Gradle, SBT or Go before, Cargo should be very familiar.