Better Programming

Advice for programmers.

Follow publication

The Essential Cargo Functions You Should Know To Use Rust

Eugene CHOI
Better Programming
Published in
4 min readJul 3, 2022
Photo by Chris Pagan on Unsplash

Cargo is a Build System and Package manager of Rust. like pip in Python or npm in JavaScript.

This means, cargo is a tool that makes us easily download external crates or manage dependencies with a single line command.

Many people are confused with the following three tools.

  • rustup: a toolchain installer for the systems of programming language RUST.
  • rsutc: a compiler of RUST
  • cargo: a package manager for packages of RUST

Let’s find out what functions cargo has:

version

This command shows the version of the cargo installed. the following three commands show the same results.

$ cargo version
cargo 1.59.0 (49d8809dc 2022-02-10)
$ cargo --version
cargo 1.59.0 (49d8809dc 2022-02-10)
$ cargo -V
cargo 1.59.0 (49d8809dc 2022-02-10)

new

This command creates a new binary or library rust package.

--binary

This argument means creating an executable binary package. If you do not specify a package type, cargo creates a binary package by default.

$ cargo new "package-name" (--bin)

parentheses () mean optional arguments, not mandatory.

new binary Example

$ cargo new hello-cargo
Created binary (application) `hello-cargo` package
$ cargo new hello-cargo2 --bin
Created binary (application) `hello-cargo2` package

--library

This argument is used to create a library project. If there is no project type argument, the binary package is created by default as I said.

$ cargo new "binary-name" --lib

new library Example

$ cargo new hello-cargo3 --lib
Created library `hello-cargo3` package

--vcs

A package created by cargo is a git directory. If you don’t want to use VCS(Version Control System) or want to change other than git, you can use vcs arguments.

$ cargo new "package-name" --vsc "vcs-name"

change vcs Example

$ cargo new hello-cargo4 --vcs git
Created binary (application) `hello-cargo4` package

$ cargo new hello-cargo5 --vcs none
Created binary (application) `hello-cargo5` package

--name

You can use --name argument if you want to have a different package name from the directory name. Of course, you can change it by editing Cargo.toml

$ cargo new "folder-name" --name "package-name"

init

It’s almost like new. The only difference is that new creates a new directory and init creates a package in the existing directory.

$ mkdir hello-cargo6 $ cd hello-cargo6$ cargo init
Created binary (application) package

--name

This is the same as cargo new --name

search

The Rustacean share their crates by crates.io. such as PyPI in Python

cargo search command can search for crates containing specific keywords.

cargo search "crate-name"

search Example

For example, if you want to find an EXIF parser crate, you can get the following results through the command cargo search exif .

$ cargo search exif
exif = "0.0.1" # Rust wrapper for libexif
kamadak-exif = "0.5.4" # Exif parsing library written in pure Rust
s.
imagemeta = "0.1.0" # Support for manipulating image metadata (exif, etc) in Rust.
img-parts = "0.2.3" # Low level crate for reading and writing Jpeg, Png and RIFF image containers
exifmv = "0.1.3" # Moves images into a folder hierarchy based on EXIF tags
gexiv2-sys = "1.1.2" # This library provides Rust FFI declarations for the gexiv2 library, which is a GObject-based wrapper …
rexiv2 = "0.9.1" # This library provides a Rust wrapper around the gexiv2 library, which is a GObject-based wrapper arou…
... and 24 crates more (use --limit N to see more)

install

You can easily install external crates found on crates.io through cargo install command.

cargo install "binary-name"

install Example

This is an example install ripgrep crate which is a grep library implemented using Rust

$ cargo install ripgrep
Updating crates.io index
Downloaded ripgrep v13.0.0
Downloaded 1 crate (272.1 KB) in 0.85s
Installing ripgrep v13.0.0
.
.
.
Finished release [optimized + debuginfo] target(s) in 32.99s
Installing C:\Users\d2h10s\.cargo\bin\rg.exe
Installed package `ripgrep v13.0.0` (executable `rg.exe`)

Cargo.toml

The tree of the newly created package is as follows.

.
│ .gitignore
│ Cargo.toml

└─src
main.rs

You could see Cargo.toml file always. This is a setting file of the cargo package and contains package name, package version you defined, rust edition, etc.

In [dependencies] field, you can describe crates your package has dependencies on.

[package]
name = "exif-fixer"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]

add crate dependency Example

For example, If you want to use kamadak-exif crate, you have to write it as follows.

[package]
name = "exif-fixer"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]kamadak-exif = "0.5.4"

and then, when you run cargo build , cargo downloads external crates automatically.

$ cargo build
Updating crates.io index
Downloaded mutate_once v0.1.1
Downloaded kamadak-exif v0.5.4
Downloaded 2 crates (56.0 KB) in 0.48s
Compiling mutate_once v0.1.1
Compiling kamadak-exif v0.5.4
Compiling exif-fixer v0.1.0 (C:\Users\d2h10s\repository\exif-fixer)
Finished dev [unoptimized + debuginfo] target(s) in 3.06s

check

Sometimes, we need to check grammar without compiling, then use cargo check command.

this command doesn’t compile and make executable binary files.

just check grammar quickly.

$ cargo check
Checking mutate_once v0.1.1
Checking kamadak-exif v0.5.4
Checking exif-fixer v0.1.0 (C:\Users\d2h10s\repository\exif-fixer)
Finished dev [unoptimized + debuginfo] target(s) in 0.72s

build

Finally, you can build a rust package with cargo build command.

Build means creating an executable binary file.

$ cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.01s

run

The run command does both build and execute. of course, If the codes have not changed since the last build, the Cargo will only run without rebuilding.

$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target\debug\hello-world.exe`
Hello, world!

No responses yet

Write a response