跳到主要内容

🌀 Rust

An annex installs rust and cargo packages locally inside the plugin or snippet directories. The crate can create so-called shims – scripts that are exposed to the standard $PATH.

Example of the shim script:

#!/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" "$@"}lsd "$@"

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

Usage of the annex

The Zi annex provides ice-modifiers rustup and cargo'…'.

The first one installs rust inside the plugin's folder using the official rustup installer and the second one has the following syntax:

cargo'[{name-of-the-binary-or-path} <-] [[!][c|n|e|o]:]{crate-name} [-> {shim-script-name}]'

Use case examples

Set up rust and the lsd crate with a shim lsd exposing the binary:

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

Set up rust and the exa crate with a shim ls exposing the exa binary:

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

Set up rust and the exa and lsd crates:

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

Set up rust, then the exa and lsd crates, with their binaries exposed by altering $PATH:

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

Set up rust and then the exa crate with shim standard error redirected to /dev/null:

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

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/0

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/0

Flags:

  • 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

Add the following snippet in the .zshrc file:

zi light z-shell/z-a-rust

This will register the rustup and cargo'…' ice-modifiers.