Rust

How Anvil runs Rust projects with cargo, and installs Rust through rustup inside its own folder.

What Anvil detects

A folder with a Cargo.toml. PHP is recognized first; see the detection table.

Anvil runs cargo run on a private port passed in $PORT and proxies <name>.test to it. The program must listen on that port, on 127.0.0.1.

Installing Rust

Settings › Languages › Rust › Install, or:

anvil runtime install rust stable

Anvil downloads rustup-init, checks rustup's published SHA-256, and runs:

rustup-init -y --no-modify-path --profile minimal --default-toolchain stable

with RUSTUP_HOME=~/.anvil/runtimes/rust/rustup and CARGO_HOME=~/.anvil/runtimes/rust/cargo. Your shell files are untouched; no rustfmt or clippy.

Choosing the version

There is one install, the stable toolchain, used by every Rust application.

Running an application

The server doesn't start on its own: Start in the Dev tab, or anvil app start myapi while the desktop app is open (this also starts dev processes marked Dev start). To start it with Anvil, turn on Start with Anvil in Settings or run anvil app autostart myapi true (off by default; with the CLI alone, restart anvil start). It runs until stopped or Anvil quits; while stopped, <name>.test shows a "not running" page.

Start command and port: Start command and Port in Settings, or:

anvil app command myapi 'cargo run --release'
anvil app port myapi 41500

No value resets to the default. The automatic port is derived from the name, in 41000–44999, moving up on collision. A program with a fixed port: set that port with anvil app port myapi <port> or Port.

VariableValue
PORTThe application's private port
HOST, HOSTNAME127.0.0.1
RUSTUP_HOME~/.anvil/runtimes/rust/rustup
CARGO_HOME~/.anvil/runtimes/rust/cargo
PATH~/.anvil/runtimes/rust/cargo/bin first

In your terminal

No cargo or rustc terminal command. To use Anvil's Rust:

export RUSTUP_HOME="$HOME/.anvil/runtimes/rust/rustup"
export CARGO_HOME="$HOME/.anvil/runtimes/rust/cargo"
export PATH="$CARGO_HOME/bin:$PATH"

Caches and files

The registry and crates live in CARGO_HOME; ~/.cargo and ~/.rustup are untouched. The project's target folder shows as rebuildable in Storage. Anvil writes nothing else into a Rust project.

New applications

anvil new rust myapi

Or New… (⌘N) in the app, under Other. Needs Rust installed. Runs cargo new --vcs none <name> and replaces src/main.rs with a small HTTP server answering "Hello from Rust" on $PORT. Takes --git/--no-git and --https/--no-https; see New applications.

Differences from production

  • cargo run makes a debug build unless you change the start command.

Removing Rust

Hover Rust stable in Settings › Languages › Rust and click Uninstall, or anvil runtime uninstall rust stable. This deletes all of ~/.anvil/runtimes/rust: rustup, every toolchain and cargo's registry. If an application was pinned to stable with anvil app runtime, it's refused until you run anvil app runtime <site> without a version.

On this page