Table of Contents
- Known issues
- Feature guide
- highlighting with tree-sitter
- Other recommended packages
- Optional features
- For package maintainers
This mode provides:
- Syntax highlighting (for Font Lock Mode)
- 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.
If you are missing features in rust-mode, please check out rustic before you open a feature request. It depends on rust-mode and provides additional features. This allows us to keep rust-mode light-weight for users that are happy with basic functionality.
adaptive-wrap-prefix-modecan 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
M-x package-install rust-mode
And put this in your config to load rust-mode automatically:
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) (add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))
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)))
Since Emacs ≥ 24.4,
electric-indent-mode is turned on by
default. If you do not like it, call
(electric-indent-mode 0) in
rust-format-buffer function will format your code with
rustfmt if installed. By
default, this is bound to C-c C-f.
rust-format-on-save enables automatic formatting on
save. For example, add the following in your init.el to enable format
(setq rust-format-on-save t)
You can toggle prettification of your code by running
M-x prettify-symbols-mode. If you'd like to automatically enable this
for all rust files, add the following to your init.el.
(add-hook 'rust-mode-hook (lambda () (prettify-symbols-mode)))
You can add your own prettifications to
For example, to display
x∔(y), simply add to your init
(push '(".add" . ?∔) rust-prettify-symbols-alist).
Running / testing / compiling code
shell out to Cargo to run, test, build and check your code. Under the
hood, these use the standard Emacs
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)
Clippy, a linter.
Easy insertion of dbg!
rust-dbg-wrap-or-unwrap either wraps or unwraps the current region
dbg!. This can be useful for easily adding debug lines to your
This is bound to C-c C-d by default.
rustic-toggle-mutabilitytoggle mut for var defined at current line
highlighting with tree-sitter
You should take a look at tree-sitter. When the dependencies are installed you can activate the feature with:
(use-package tree-sitter :config (require 'tree-sitter-langs) (global-tree-sitter-mode) (add-hook 'tree-sitter-after-on-hook #'tree-sitter-hl-mode))
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)
Note that racer and rls are considered deprecated. You should try rust-analyzer instead.
Other recommended packages
flycheck allows highlighting compile errors and Clippy lints inline.
cargo.el provides a minor mode for integration with Cargo, Rust's package manager.
rustic is based on rust-mode, extending it with other features such as integration with LSP and with flycheck.
The features of the following files can be disabled with
They are disabled by default when you use rustic as it has its own implementations for those features.
rust-cargo-default-arguments set additional cargo args used for check,compile,run,test
For package maintainers
Run elisp tests:
Contributions are very welcome. We are also looking for additional maintainers.