hamacs/ha-programming-rust.org
Howard Abrams 6e92e3d13d New Mac system shook out some MacOS changes needing incorporation
Specifically how Homebrew is installing its goodies in a different
location, which makes it a bit incompatible with my Linux boxen.
2023-11-06 09:45:45 -08:00

5.4 KiB
Raw Blame History

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. Im intrigued with Rust by Example, and find that it reads well in an Emacs EWW buffer. Im also interested in seeing if the Rust Koans project is complete, as I love that style.

Getting Started

Lets curtail the standard installation, as Ive 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

Lets 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, well do:

  brew install rust-analyzer

Configuration

Lets 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 '("/opt/homebrew/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)
  1. 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).
  2. Add your code then press Ctl-Return (it bound to rust-playground-exec command). It will save, compile and exec the snippet code.
  3. 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)

LSP