Isma Arenzana

Emacs for Rust Development

Posted at — Jan 11, 2022

Emacs > Visual Studio Code

There's a good chance I'm a hater or that I don't know what I'm doing. After a couple of hours trying to set up VS Code for Rust development, I ended up frustrated not understanding how to simply call the compiler or the debugger from the interface. I looked for rust-mode on Github and 15 minutes later I was up and running on Emacs. Maybe it's my previous experience with Go development set ups that made it all quite simple.

Rust on Emacs

I approached this the same way I approached Go: let's get regular syntax highlighting, linting, formatting, and compiling first and we'll worry about debuggers later. I'm a bit old school when it comes to approaching debugging. We end up with the following on the Emacs config (if you use use-package):

  (use-package rust-mode
    :defer t
    :ensure t
    :init
      (add-hook 'rust-mode-hook
              (lambda () (setq indent-tabs-mode nil)))
      (setq rust-format-on-save t)
      (add-hook 'rust-mode-hook
          (lambda () (prettify-symbols-mode)))
      (add-hook 'rust-mode-hook #'lsp)
    :bind (("M-," . rust-compile))
  )
  (use-package flycheck-rust
  :ensure t
  :hook (flycheck-mode-hook . flycheck-rust-setup))

Here, we load the mode for Rust development. Auto-indent with spaces, run rustfmt on save (needs to be installed via rustup. Prettify symbols (cause why not), and use language server protocol. I'm binding "M-," to compile; I know it's an odd keybinding, but I'm used to it by now. Feel free to make it your own.

In addition to this, I'm hooking flycheck for linting.

Conclusion

IntelliJ made it very easy to set up as well, but that's a multi-hundred dollar application. If you're on a budget, Emacs, vim, or VSCode should bet viable options as well.

I'm still getting started with Rust and I don't know how much it will stick, since I've been writing Go for a number of years; but I'm happy I have a familiar environment to get started from. By now I should know Emacs should always be my first option.