My EMACS Rust IDE

In this post, I will walk you through configuring an EMACS Rust IDE to get syntax highlighting and code completion  for the Rust programming language.

Naturally, the first thing you need to do is have Emacs configured.  If you don’t then checkout my post Install EMACS 26.1 on Ubuntu.

You will also need to make sure that you have Rust installed.  If you don’t then follow my article Install Rust on Ubuntu 18.04.

This post is directed at complete Emacs beginners so keep that in mind as you go through this article.

Video

I created a video that demonstrates this in practice:

[video_player type=”youtube” youtube_show_title_bar=”Y” style=”1″ dimensions=”560×315″ width=”560″ height=”315″ align=”center” margin_top=”0″ margin_bottom=”20″ ipad_color=”black”]aHR0cHM6Ly95b3V0dS5iZS9BYXdfODRuT0F1SQ==[/video_player]

Ensure MELPA is Enabled

Edit the Emacs configuration file:

# emacs ~/.emacs

Ensure that the following lines are there

(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
                    (not (gnutls-available-p))))
       (proto (if no-ssl "http" "https")))
  (add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t) 
  (when (< emacs-major-version 24)
    (add-to-list 'package-archives '("gnu" . (concat proto "://elpa.gnu.org/packages/")))))
(package-initialize)

Save your file.

C-x C-s

Update your package list

M-x package-refresh RET

Install Emacs Rust Major Mode

After it is done we need to install some packages

M-x package-install RET rust-mode RET
M-x package-install flymake-rust RET

You now have syntax highlighting and syntax checking for rust files

Code Completion

To get code completion we need to use Racer.  First, we need to install racer:

$ rustup component add rust-src
$ cargo install racer

Back in Emacs install the Racer and Company packages:

M-x package-install RET racer RET
M-x package-install RET company RET

Add a line to your ~/.emacs file

(add-hook 'rust-mode-hook )
(add-hook 'racer-mode-hook )

You now have code completion for Rust!

Emacs Cargo Mode

Emacs even has a mode for cargo! Install the package:

M-x package-install cargo

Tell Emacs to load the cargo minor mode when you load a Rust file

(add-hook 'rust-mode-hook 'cargo-minor-mode)

The docs state that you can use this mode to create new cargo packages but from my experience I got an error every time.  I have created an issue and hopefully they will tell me what I did wrong.

For now you will need to create a cargo package from the command line:

$ cargo new admintome
Created binary (application) `admintome` project
[email protected]:~/Development/rust$

Change to the new directory then you can edit the source file located at

src/main.rs

using

$ emacs src/main.rs

This mode has a ton of options but I will only cover what I think are the most important.  For a full list checkout the cargo-mode repo README.

cargo build
C-c C-c C-b

cargo run
C-C C-c C-r

Here is an example of running the file:

emacs rust mode

Congrats you now have a pretty decent emacs rust IDE and can code away.

Conclusion

I hope you have enjoyed this article, if so please leave a comment below.  For more articles, please signup for the AdminTome Blog below.  Also please feel free to share the article to your friends using the buttons to the left.  Thanks again for