5.4 KiB
Rust Configuration
A literate programming file for configuring Rust with Emacs.
Learning Rust
I appreciate that the rust-lang.org recommends three ways to learn the language. I’m intrigued with Rust by Example, and find that it reads well in an Emacs EWW buffer. I’m also interested in seeing if the Rust Koans project is complete, as I love that style.
Getting Started
Let’s curtail the standard installation, as I’ve read I could use rustup
as it will allow my to switch between versions of Rust without having to download specific packages (otherwise, one would, on the Mac at least, issue a brew install rust
):
brew install rustup
Use the rustup
to install the Rust compiler (rustc
) and the Rust package manager (cargo
):
rustup-init
To verify, run:
rustc --version
Let’s get the rest of the Rust packages for development installed:
cargo install rustfmt
Install the rust-analyzer is an Rust LSP server implementation (see these installation instructions). For Mac, we’ll do:
brew install rust-analyzer
Configuration
Let’s find the rust
binaries:
(add-to-list 'exec-path (file-name-concat (getenv "HOME") ".cargo/bin"))
The rustic project incorporates rust-mode and provides these features:
- cargo popup
- multiline error parsing
- translation of ANSI control sequences through xterm-color
- async org babel
- automatic LSP configuration with eglot or lsp-mode
- cask for testing
(use-package rustic
:init
(setq rustic-lsp-client 'eglot
rustic-analyzer-command '("/usr/local/bin/rust-analyzer"))
;; :bind (:map rustic-mode-map
;; ("M-j" . lsp-ui-imenu)
;; ("M-?" . lsp-find-references)
;; ("C-c C-c l" . flycheck-list-errors)
;; ("C-c C-c a" . lsp-execute-code-action)
;; ("C-c C-c r" . lsp-rename)
;; ("C-c C-c q" . lsp-workspace-restart)
;; ("C-c C-c Q" . lsp-workspace-shutdown)
;; ("C-c C-c s" . lsp-rust-analyzer-status)
;; ("C-c C-c e" . lsp-rust-analyzer-expand-macro)
;; ("C-c C-c d" . dap-hydra)
;; ("C-c C-c h" . lsp-ui-doc-glance))
:config
(setq rustic-format-on-save t))
The Playground
The rust-playground project can create / cleanup rust scratch projects quickly:
(use-package rust-playground)
- From any mode run
M-x rust-playground
for start a new playground buffer filled with basic template for the package with main function (see the picture below). - Add your code then press
Ctl-Return
(it bound torust-playground-exec
command). It will save, compile and exec the snippet code. - When you played enough with this snippet just run
M-x rust-playground-rm
. It will remove the current snippet with its directory and all files.
Cargo and Toml
The toml-mode adds syntax highlighting for toml files.
(use-package toml-mode)