Table of Contents
- rust-mode
rust-mode
makes editing Rust code with Emacs
enjoyable. It requires Emacs 25 or later, and is included in both
Emacs Prelude and
Spacemacs by default.
This mode provides:
- Syntax highlighting (for Font Lock Mode)
- Indentation
- Integration with Cargo, clippy and rustfmt
This mode does not provide autocompletion, or jumping to function / trait definitions. See Auto-completion / code navigation below for tips on how to enable this.
rust-syntax-propertize
andadaptive-wrap-prefix-mode
can lead to severe lag when editing larger files (brotzeit/rustic#107)
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)
NonGNU ELPA can be used out of the box in emacs28.
For older versions you need to add something like the following to your init file:
(with-eval-after-load 'package (add-to-list 'package-archives '("nongnu" . "https://elpa.nongnu.org/nongnu/")))
Clone this repository locally, and add this to your init.el:
(add-to-list 'load-path "/path/to/rust-mode/")
(autoload 'rust-mode "rust-mode" nil t)
Commands like TAB should indent correctly.
The Rust style guide recommends spaces rather than tabs for indentation; to follow the recommendation add this to your init.el, which forces indentation to always use spaces.
(add-hook 'rust-mode-hook
(lambda () (setq indent-tabs-mode nil)))
The rust-format-buffer
function will format your code with
rustfmt if installed. By
default, this is bound to C-c C-f.
The variable rust-format-on-save
enables automatic formatting on
save. For example, add the following in your init.el to enable format
on save:
(setq rust-format-on-save t)
The rust-run
, rust-test
, rust-compile
and rust-check
functions
shell out to Cargo to run, test, build and check your code. Under the
hood, these use the standard Emacs compile
function.
These are not bound by default. To bind these to keyboard shortcuts, you can use the following in your init.el:
(define-key rust-mode-map (kbd "C-c C-c") 'rust-run)
rust-run-clippy
runs
Clippy, a linter.
rust-dbg-wrap-or-unwrap
either wraps or unwraps the current region
in dbg!
. This can be useful for easily adding debug lines to your
program.
This is bound to C-c C-d by default.
rustic-toggle-mutability
toggle mut for var defined at current line
A lightweight lsp client.
(add-hook 'rust-mode-hook 'eglot-ensure)
Provides more features and you can enhance the functionality by using additional packages. You can find more information in the lsp-mode wiki.
(add-hook 'rust-mode-hook #'lsp)
You can either use a lsp client or racer with emacs-racer.
flycheck allows highlighting compile errors and Clippy lints inline.
cargo.el provides a minor mode for integration with Cargo, Rust's package manager.
cargo-mode is an Emacs minor mode which allows to dynamically select a Cargo command. The reasons behind this package can be found in the post.
rustic is based on rust-mode, extending it with other features such as integration with LSP and with flycheck.
Run elisp tests:
make test
Contributions are very welcome. We are also looking for additional maintainers.