Rust Playground At Your Fingertips
2021-03-12Coming to Rust from Ruby I miss the interactive console (IRB) lot.
Sometimes I go to Rust Plyaground online to test simple things,
but more often I go to /tmp
directory, create a new rust project, open Vim, run cargo watch -x run
and play.
It's very frequent scenario, why not automate it? This way I came to
a little shell script that relies on tmux
, cargo-watch
and nvim
.
#!/bin/sh
PLAYGROUNDS_DIR="/tmp/rust-playgrounds"
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
PROJECT_DIR="${PLAYGROUNDS_DIR}/playground${TIMESTAMP}"
cargo new $PROJECT_DIR
cd $PROJECT_DIR
# Add dependencies to Cargo.toml
for CRATE in $@; do
sed "/^\[dependencies\]/a $CRATE = \"*\"" -i Cargo.toml
done
tmux new-session \; \
send-keys "nvim ./src/main.rs" C-m \; \
split-window -h \; \
send-keys "cargo watch -s 'clear && cargo run -q'" C-m \; \
select-pane -L;
I saved as playground
executable and put it into a directory with other binaries.
Now let's say I want to play with whatlang crate, then I just type:
playground whatlang
What I get is a project with whatlang
dependency in Cargo.toml
opened and running automatically.
I just need to type, save, and the result is shown immediately.