Rust Playground At Your Fingertips

Coming 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.


TIMESTAMP=$(date +"%Y%m%d%H%M%S")

cargo new $PROJECT_DIR

# Add dependencies to Cargo.toml
for CRATE in $@; do
    sed "/^\[dependencies\]/a $CRATE = \"*\"" -i Cargo.toml

tmux new-session \; \
  send-keys "nvim ./src/" 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.

Rust Playground with tmux and vim