Rust layer
Table of ContentsClose
1. Description
This layer supports Rust development in Spacemacs.
1.1. Features:
- Auto-completion and navigation support through lsp-mode
- Interactive debugger using dap-mode
- Support for the Rust package manager Cargo
- Support for Rusty Object Notation (RON)
2. Install
2.1. Layer
To use this configuration layer, add it to your ~/.spacemacs
. You will need to
add rust
to the existing dotspacemacs-configuration-layers
list in this
file.
The toml
layer is automatically loaded when using this layer.
2.2. LSP
The lsp
backend will be automatically enabled if the layer lsp
is used.
2.2.1. Autocompletion
To enable auto-completion, ensure that the auto-completion
layer is enabled.
2.2.2. Debugger (dap integration)
To install the debug adapter you may run M-x dap-gdb-lldb-setup
when you are on Linux or download it manually from Native Debug and adjust dap-gdb-lldb-path
.
2.2.3. Reloading Workspace
By default, rust-analyzer will automatically reload the workspace
after you make changes to Cargo.toml
(such as adding dependencies).
However, if lsp-rust-analyzer-cargo-auto-reload
is set to nil, you
will need to reload the workspace manually for the LSP to notice those
changes. You can call spacemacs/lsp-rust-analyzer-reload-workspace
,
which would be faster than restarting the LSP backend.
You can configure it in your ~/.spacemacs
.
(rust :variables lsp-rust-analyzer-cargo-auto-reload t rustic-format-on-save t)
2.3. Cargo
Cargo is a project management command line tool for Rust. Installation instructions can be found on the main page of Cargo.
2.3.1. cargo-edit
cargo-edit allows you to add, remove, and upgrade dependencies by modifying your Cargo.toml
file.
cargo install cargo-edit
2.3.2. cargo-outdated
cargo-outdated displays dependencies that have new version available.
cargo install cargo-outdated
2.4. Rustfmt
Format Rust code according to style guidelines using rustfmt.
rustup component add rustfmt
To enable automatic buffer formatting on save, set the variable rustic-format-on-save
to t
.
2.5. Clippy
Clippy provides a collection of lints to catch common mistakes and improve your code.
rustup component add clippy
2.6. Analyzer
Analyzer provides IDE-like features for Rust.
rustup component add rust-analyzer
3. Key bindings
Key binding | Description |
---|---|
SPC m = = |
reformat the buffer |
SPC m b R |
reload Rust-Analyzer workspace |
SPC m c . |
rerun the default binary with the same arguments |
SPC m c = |
format all project files with rustfmt |
SPC m c a |
add a new dependency with cargo-edit |
SPC m c c |
compile project |
SPC m c C |
remove build artifacts |
SPC m c d |
generate documentation and open it in default browser |
SPC m c s |
search the documentation |
SPC m c e |
run benchmarks |
SPC m c i |
initialise a new project with Cargo (init) |
SPC m c l |
run linter (cargo-clippy) |
SPC m c f |
run linter automatic fixes (cargo-clippy) |
SPC m c n |
create a new project with Cargo (new) |
SPC m c o |
display outdated dependencies (cargo-outdated) |
SPC m c r |
remove a dependency with cargo-edit |
SPC m c u |
update dependencies with Cargo |
SPC m c U |
upgrade dependencies to LATEST version with cargo-edit |
SPC m c v |
check (verify) a project with Cargo |
SPC m c x |
execute the default binary |
SPC m g g |
jump to definition |
SPC m h h |
describe symbol at point |
SPC m s s |
switch to other LSP server backend |
SPC m t a |
test current project |
SPC m t t |
run the current test |
3.1. Debugger
Using the dap
layer you'll get access to all the DAP key bindings, see the
complete list of key bindings on the dap layer description.