Jump to content

Rust: Difference between revisions

From Bath Wiki
Translate from boss.bathcs.com
 
Update and add sections on useful tools.
Line 1: Line 1:
The rust compiler is pretty brutal so not much has to go in here.
The Rust compiler is pretty pedantic by default, however, we have a few recommendations:


* Change the default check command in VSCode (instead of check use <code>clippy</code>)
* Change the default check command in VSCode (instead of check use <code>clippy</code>).
* 4 spaces for tabs
* 4 spaces for tabs.
* Macros are cool but don’t overuse them.
* Macros are cool but don’t overuse them.
* Use rustfmt: <code>cargo fmt</code>.


== Use rust analyzer + clippy ==
= Recommended tools =
== Bacon ==
Get [https://github.com/Canop/bacon bacon], open it in another terminal window, leave it running while you edit. It will save you recompiling constantly to get the verbose compiler output.


Rust analyzer is great and has a bunch of recommendations which you should follow, so set this up and use it.
It also has handy features:


== Use rustfmt ==
* <code>a</code> - to check all.
* <code>c</code> - to clippy all.
* <code>t</code> - to run tests.
* <code>n</code> - to run tests via [https://nexte.st/ <code>cargo-nextest</code>].


This just standardises the code format.
You can also make it run pedantic checks with <code>bacon pedantic</code>.
 
== Audit ==
<code>cargo install cargo-audit</code> (or use [https://github.com/cargo-bins/cargo-binstall <code>cargo-binstall</code>]).
 
You can then run <code>cargo audit</code> to audit your dependencies for security vulnerabilities.
 
== Flamegraph ==
<code>cargo install cargo-flamegraph</code> (or use [https://github.com/cargo-bins/cargo-binstall <code>cargo-binstall</code>]).
 
You can then use <code>CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph [-- args-for-your-project]</code> to generate a [https://www.brendangregg.com/flamegraphs.html Flamegraph] for profiling and optimising your project.
 
== Machete ==
<code>cargo install cargo-machete</code> (or use [https://github.com/cargo-bins/cargo-binstall <code>cargo-binstall</code>]).
 
You can then use <code>cargo machete</code> to remove unused dependencies.

Revision as of 00:25, 12 June 2026

The Rust compiler is pretty pedantic by default, however, we have a few recommendations:

  • Change the default check command in VSCode (instead of check use clippy).
  • 4 spaces for tabs.
  • Macros are cool but don’t overuse them.
  • Use rustfmt: cargo fmt.

Recommended tools

Bacon

Get bacon, open it in another terminal window, leave it running while you edit. It will save you recompiling constantly to get the verbose compiler output.

It also has handy features:

  • a - to check all.
  • c - to clippy all.
  • t - to run tests.
  • n - to run tests via cargo-nextest.

You can also make it run pedantic checks with bacon pedantic.

Audit

cargo install cargo-audit (or use cargo-binstall).

You can then run cargo audit to audit your dependencies for security vulnerabilities.

Flamegraph

cargo install cargo-flamegraph (or use cargo-binstall).

You can then use CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph [-- args-for-your-project] to generate a Flamegraph for profiling and optimising your project.

Machete

cargo install cargo-machete (or use cargo-binstall).

You can then use cargo machete to remove unused dependencies.