Skip to main content

🌀 Rust


An annex installs rust and cargo packages locally inside the plugin or snippet directories. The crate can then have a so-called shim created (a name borrowed from rbenv) – a script that's located in the standard $PATH entry "`$ZPFX/bin" of the following contents (example):

#!/usr/bin/env zshfunction lsd { local bindir="/root/.zi/plugins/z-shell---null/bin" local -x PATH="/root/.zi/plugins/z-shell---null"/bin:"$PATH" # -x means export local -x RUSTUP_HOME="/root/.zi/plugins/z-shell---null"/rustup CARGO_HOME="/root/.zi/plugins/z-shell---null" "$bindir"/"lsd" "[email protected]"}lsd "[email protected]"

As it can be seen shim ultimately provides the binary to the command line.

Usage of the annex

The ZI Annex provides two new ices: rustup and cargo''. The first one installs rust inside the plugin's folder using the official rustup installer. The second one has the following syntax:

cargo"[name-of-the-binary-or-path <-] [[!][c|N|E|O]:]{crate-name} [-> {shim-script-name}]'

Example uses are:

Installs rust and then the lsd' crate and creates thelsd' shim exposing the binary:

zi ice rustup cargo'!lsd'zi load z-shell/null

Installs rust and then the exa' crate and creates thels' shim exposing the`exa' binary:

zi ice rustup cargo'!exa -> ls'zi load z-shell/null

Installs rust and then the exa' andlsd' crates:

zi ice rustup cargo'exa;lsd'zi load z-shell/null

Installs rust and then the exa' andlsd' crates and exposes their binaries by altering $PATH

zi ice rustup cargo'exa;lsd' as"command" pick"bin/(exa|lsd)"zi load z-shell/null

Installs rust and then the `exa' crate and creates its shim with standard error redirected to /dev/null

zi ice rustup cargo'!E:exa'zi load z-shell/null

Just install rust and make it available globally in the system

zi ice id-as"rust" wait"0" lucid rustup as"command" pick"bin/rustc" atload="export \  CARGO_HOME=\$PWD RUSTUP_HOME=\$PWD/rustup"zi load z-shell/null

A little more complex rustup configuration that uses Bin-Gem-Node annex and installs the cargo completion provided with rustup, using the for syntax:

zi id-as=rust wait=1 as=null sbin="bin/*" lucid rustup \  atload="[[ ! -f ${ZI[COMPLETIONS_DIR]}/_cargo ]] && zi creinstall rust; \  export CARGO_HOME=\$PWD RUSTUP_HOME=\$PWD/rustup" for \z-shell/null

Flags meanings:

  • N – redirect both standard output and error to /dev/null
  • E – redirect standard error to /dev/null
  • O – redirect standard output to /dev/null
  • c – change the current directory to the plugin's or snippet's directory before executing the command

As the examples showed, the name of the binary to run and the shim name are by default equal to the name of the crate. Specifying {binary-name} <- … and/or … -> {shim-name} allows to override them.

Install rust annex

Simply load like a regular plugin, i.e.:

zi light z-shell/z-a-rust

This installs the annex and makes the rustup and cargo'…' ices available.

Rust tools meta plugin

To install Rust utilities, simply run zi light @rust-utils, it will install and setup as follows:

bin-gem-node annex: Created the cargo shim and set +x on the cargo binarybin-gem-node annex: Created the cargo-clippy shim and set +x on the cargo-clippy binarybin-gem-node annex: Created the cargo-fmt shim and set +x on the cargo-fmt binarybin-gem-node annex: Created the cargo-miri shim and set +x on the cargo-miri binarybin-gem-node annex: Created the clippy-driver shim and set +x on the clippy-driver binarybin-gem-node annex: Created the rls shim and set +x on the rls binarybin-gem-node annex: Created the rustc shim and set +x on the rustc binarybin-gem-node annex: Created the rustdoc shim and set +x on the rustdoc binarybin-gem-node annex: Created the rustfmt shim and set +x on the rustfmt binarybin-gem-node annex: Created the rust-gdb shim and set +x on the rust-gdb binarybin-gem-node annex: Created the rust-lldb shim and set +x on the rust-lldb binarybin-gem-node annex: Created the rustup shim and set +x on the rustup binary