Emacs mode for editing Rust source code
Table of Contents
Installation
Melpa
rust-mode
makes editing Rust code with Emacs
enjoyable. It requires Emacs 24 or later.
The package is available on MELPA. Add this to your init.el.
(require 'package)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(package-refresh-contents)
Now you can install rust-mode
with:
M-x package-install rust-mode
And put this in your config to load rust-mode automatically:
(require 'rust-mode)
straight
straight.el clones each of your packages directly from its source. There are good additional installation instructions for moving your package management from package.el to straight.
Manual Installation
Add this to your init.el:
(add-to-list 'load-path "/path/to/rust-mode/")
(autoload 'rust-mode "rust-mode" nil t)
Indentation
The Rust style guide recommends spaces for indentation; to follow the recommendation add this to your init.el:
(add-hook 'rust-mode-hook
(lambda () (setq indent-tabs-mode nil)))
rustfmt
The rust-format-buffer
function will format your code with
rustfmt if installed. By default,
this is bound to C-c C-f
.
Placing (setq rust-format-on-save t)
in your init.el will enable automatic
running of rust-format-buffer
when you save a buffer.
Tests
The file rust-mode-tests.el
contains tests that can be run via
ERT.
You can use run_rust_emacs_tests.sh
to run them in batch mode, if
you set the environment variable EMACS to a program that runs emacs.
LSP
eglot
lsp-mode
You can find more information in the lsp-mode wiki.
Other useful packages
- cargo.el Emacs Minor Mode for Cargo, Rust's Package Manager
- emacs-racer Racer support for Emacs
- rustic Rust development environment for Emacs