Skip to content

Instantly share code, notes, and snippets.

@dominiwe
Last active August 13, 2025 11:08
Show Gist options
  • Select an option

  • Save dominiwe/db55aabef877d24d603a3d50723162e0 to your computer and use it in GitHub Desktop.

Select an option

Save dominiwe/db55aabef877d24d603a3d50723162e0 to your computer and use it in GitHub Desktop.
Helper functions for rust development

Helper functions for rust development

These are helper functions for rust development inspired by Jeremy Chones cwt and cwe helper functions (however I call them rwt and rwe). They start examples or tests and watch them with bacon. This is nice when exploring in examples or doing a kind of test driven development.

These are best if you have fzf, bacon and bat installed but should also work with just bacon (that is needed and recommended, as cargo-watch is unmaintained). Add the function declarations in rust_helpers.bash to your initialization files (e.g. ~/.bashrc). To also get autocompletion for them, create files ~/.local/share/bash-completion/completions/{rwe,rwt} and add the contents of rwe_completion.bash and rwt_completion.bash. Note that the completion files have no file extension. I added an extension here so that the gist renders more nicely.

Just call rwe or rwt to see the fzf window. For examples it should also display a preview of the code. Select a test or example and press enter to watch it.

One disclaimer about the autocompletion of rwt. It works best when typing rwt ", since test names are fully qualified and contain :: (double colons).

Preview

Selection:

first

While running:

second
# watch examples with bacon
rwe() {
if ! command -v bacon >/dev/null 2>&1; then
echo "Error: \`bacon\` command not found." >&2
return 1
fi
case $# in
0)
if ! command -v fzf >/dev/null 2>&1 || [ ! -d "examples" ]; then
echo "Usage: rwe <example_name>" >&2
[ ! -d "examples" ] && echo "Error: 'examples/' directory not found." >&2
! command -v fzf >/dev/null 2>&1 && echo "Tip: Install 'fzf' for interactive selection." >&2
return 1
fi
local selected
selected=$(find examples -name "*.rs" -type f 2>/dev/null |
while IFS= read -r file; do
local name="${file#examples/}"
name="${name%.rs}"
[[ "$name" == */main ]] && name="${name%/main}"
echo -e "$name\t$file"
done |
sort -u |
fzf --prompt="Select example: " \
--header="Select an example to run" \
--delimiter=$'\t' \
--with-nth=1 \
--preview='bat --color=always {2} 2>/dev/null || cat {2}' \
--preview-window=right:60%)
if [ -n "$selected" ]; then
local example_name="${selected%%$'\t'*}"
echo "Running example: $example_name"
bacon run -- -q --example "$example_name"
fi
;;
1)
bacon run -- -q --example "$1"
;;
*)
echo "Error: Too many arguments." >&2
echo "Usage: rwe <example_name>" >&2
return 1
;;
esac
}
# watch tests with bacon (and preserve output)
rwt() {
if ! command -v bacon >/dev/null 2>&1; then
echo "Error: \`bacon\` not found." >&2
return 1
fi
case $# in
0)
if command -v fzf >/dev/null 2>&1; then
local selected
selected=$(cargo test -q -- --list 2>/dev/null |
grep ': test$' |
sed 's/: test$//' |
fzf \
--prompt="Select test: " \
--header="Enter to run" \
--preview="echo 'Selected test:'; echo '---'; echo {}" \
--preview-window=up:30%)
if [ -n "$selected" ]; then
echo "Running: $selected"
bacon test -- "$selected" -- --nocapture
fi
else
echo "Usage: rwt [test_file] <test_filter>" >&2
echo "Tip: Install fzf for interactive test selection" >&2
return 1
fi
;;
1) bacon test -- "$1" -- --nocapture ;;
2) bacon test -- --test "$1" -- "$2" -- --nocapture ;;
*)
echo "Usage: rwt [test_file] <test_filter>" >&2
return 1
;;
esac
}
_rwe() {
local cur="${COMP_WORDS[COMP_CWORD]}"
if [ "$COMP_CWORD" -eq 1 ]; then
local examples=""
if [ -d examples ]; then
while IFS= read -r -d '' file; do
local rel_path="${file#examples/}"
local name="${rel_path%.rs}"
[[ "$name" == */main ]] && name="${name%/main}"
examples="$examples $name"
done < <(find examples -name "*.rs" -type f -print0 2>/dev/null)
fi
if [ -n "$examples" ]; then
mapfile -t COMPREPLY < <(compgen -W "$examples" -- "$cur")
fi
fi
}
complete -F _rwe rwe
_rwt() {
local cur="${COMP_WORDS[COMP_CWORD]}"
case "$COMP_CWORD" in
1)
local tests=""
if [ -d tests ]; then
tests=$(find tests -name "*.rs" -type f 2>/dev/null |
sed 's|.*/||; s|\.rs$||' | grep -v '^common$')
fi
if command -v cargo >/dev/null 2>&1; then
local test_names
test_names=$(cargo test -q -- --list 2>/dev/null |
grep '^[a-z_]' | sed 's/: .*//')
tests="$tests $test_names"
fi
if [ -n "$tests" ]; then
mapfile -t COMPREPLY < <(compgen -W "$tests" -- "$cur")
fi
;;
2)
local test_file="${COMP_WORDS[1]}"
if command -v cargo >/dev/null 2>&1; then
local test_names
test_names=$(cargo test -q --test "$test_file" -- --list 2>/dev/null |
grep '^[a-z_]' | sed 's/: .*//')
if [ -n "$test_names" ]; then
mapfile -t COMPREPLY < <(compgen -W "$test_names" -- "$cur")
fi
fi
;;
esac
}
complete -F _rwt rwt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment