A tiny adapter that lets rustfmt format Rust statement snippets used in
Markdown code blocks and REPLs.
It first runs rustfmt on the input as a complete Rust source file. If that
fails, it temporarily wraps the input in fn main(), formats it, and removes
the wrapper again.
cargo install rustfmt-replRead from stdin and write to stdout:
'let x=(1,2); dbg!(x, x.0);' | rustfmt-replOr format a file in place:
rustfmt-repl path\to\snippet.rsFor repeated formatting of many snippets, enable the opt-in content cache:
rustfmt-repl --cache path\to\snippet.rsrustfmt-repl can be used as the Rust formatter behind either
mdsf or
Panache. The orchestrator extracts Rust
code blocks; rustfmt-repl makes complete files and statement snippets usable
with rustfmt.
Use one of these integrations in a formatting chain, not both.
Add the following language entry to mdsf.json:
{
"languages": {
"rust": {
"binary": "rustfmt-repl",
"arguments": ["--cache", "$PATH"],
"stdin": false
}
}
}mdsf gives rustfmt-repl a temporary file through $PATH, so this example
uses file mode.
Add the following entries to panache.toml:
external-max-parallel = 8
[formatters]
rust = "rustfmt-repl"
rs = "rustfmt-repl"
[formatters.rustfmt-repl]
cmd = "rustfmt-repl"
args = ["--cache"]
stdin = truePanache sends each code block through stdin and can run independent code-block
formatters in parallel. rustfmt-repl still performs the snippet wrapping and
unwrapping required by rustfmt.
Make sure Cargo's binary directory is available on PATH. Its usual location
is $HOME/.cargo/bin on Unix-like systems and %USERPROFILE%\.cargo\bin on
Windows. If the editor does not inherit your shell environment, use the full
path to the executable in your local configuration.