Thursday, February 20, 2025

Rust and Cargo Cheat sheet

Installing Rust, its crates and how to keep them up to date

Jump to the rust and crate management section for a list of commonly used commands.

Introduction

A couple of years ago, I found a utility called bat that I wanted to try out and possibly use as a partial replacement to the venerable print and concatenation tool named cat. It was written in a program language called Rust which I had heard about in passing and was curious about its rising popularity. The version that was currently available for my Debian system, at the time, lagged a few versions behind the one that I had read about and lacked some of the features that had initially piqued my interest.

I was already using pyenv to manage my Python releases and, I wondered if I could do the same for Rust. I haven’t really had the time, or inclination to become a Rust programmer; but, there were some applications written in Rust that I wanted to use. I wasn’t planning on installing many applications or updating them very frequently, so I needed a place to keep commands that my muscle memory hadn’t yet absorbed.

That’s how this cheat sheet initally started.

This is a very brief guide for installing Rust and managing those crates(applications, packages, or libraries) for people who are not Rust developers and who do not use Rust on a daily basis. It is not intended as a tutorial(see the Resources section for helpful links).

Getting Started

  • The Rust Getting Started page is a good place to begin learning about Rust, its eco-system, and how to create new Rust projects. Here, I’m just going to focus on getting Rust installed and the basics of using cargo, the Rust package manager, to install and upgrade crates. I’ve only used the commands on this page with Debian Linux. So, if you have any issues using them, I would strongly recommend looking at the official Getting Started page for additional information.

Common Rust applications

  • rustc - Rust interpreter
  • rustup - Rust tool chain installer & version management tool
  • cargo - Rust build tool & package manager
  • crates.io - A Rust community registry of libraries and applications

Rust Installation

From your command prompt, run the following:

~$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

By installing this way, rustup will install itself, the Rust tool chain, as well as the cargo package manager. Rust can be uninstalled at any time using the rustup self uninstall command and these changes will be reverted.

Default directories

Rustup metadata and toolchains are installed into the Rustup home directory, located at ~/.rustup. This location can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at ~/.cargo. This location can be modified using the CARGO_HOME environment variable.

The cargo, rustc, rustup and other binaries are added to Cargo’s bin directory, located at ~/.cargo/bin.

Rust and crate management

Steps for upgrading Rust

  • Check the currently installed Rust version
    rustc -V
  • Update Rust
    rustup update
  • Update rustup
    rustup self update

Updating crates

As of Rust 1.41.0, cargo install <crate> will detect if a package is installed. It will upgrade if there is a newer version, or do nothing if the crate is considered up to date. The command will always uninstall, download, and compile the latest version of the crate - even if there is no newer version available.

The cargo-update crate

This crate creates a cargo subcommand for checking and applying updates to installed executables. It helps simplify crate management.

  • cargo-update home page
  • Install cargo-update
    cargo install cargo-update
  • Check for newer versions and update selected packages
    cargo install-update -a

Steps for installing and upgrading crates

  • List installed crates
    cargo install --list
    • Or, add an alias to .bashrc or bash_aliases:
      alias rust_list='cargo install --list
  • Install a crate
    cargo install <package_name>
  • Update all crates
    cargo install-update -a
  • Check for newer versions and update selected packages
    cargo install-update crate1 crate2 ...

Currently Installed Crates

Crates that I have currently on my system and find useful

  • bat A cat(1) clone with syntax highlighting and Git integration.
  • cargo Cargo downloads your Rust project’s dependencies and compiles your project.
  • cargo-update A cargo subcommand for checking and applying updates to installed executables
  • csview A high performance csv viewer with cjk/emoji support
  • htmlq A lightweight, command-line HTML processor
  • iamb A Matrix chat client that uses Vim keybindings
  • zizmor A Static analysis tool for GitHub Actions

Resources

No comments: