#+title:  Rust Configuration
#+author: Howard X. Abrams
#+date:   2022-08-26
#+tags: emacs rust programming

A literate programming file for configuring Rust with Emacs.

#+begin_src emacs-lisp :exports none
  ;;; ha-programming-rust --- configuring a rust environment. -*- lexical-binding: t; -*-
  ;;
  ;; © 2022-2023 Howard X. Abrams
  ;;   Licensed under a Creative Commons Attribution 4.0 International License.
  ;;   See http://creativecommons.org/licenses/by/4.0/
  ;;
  ;; Author: Howard X. Abrams <http://gitlab.com/howardabrams>
  ;; Maintainer: Howard X. Abrams
  ;; Created: August 26, 2022
  ;;
  ;; While obvious, GNU Emacs does not include this file or project.
  ;;
  ;; *NB:* Do not edit this file. Instead, edit the original literate file at:
  ;;            /Users/howard.abrams/other/hamacs/ha-programming-rust.org
  ;;       And tangle the file to recreate this one.
  ;;
  ;;; Code:
  #+end_src

* Learning Rust
I appreciate that the [[https://www.rust-lang.org/learn][rust-lang.org]] recommends three ways to learn the language. I’m intrigued with [[https://doc.rust-lang.org/stable/rust-by-example/][Rust by Example]], and find that it reads well in an Emacs EWW buffer. I’m also interested in seeing if the [[https://github.com/crazymykl/rust-koans][Rust Koans]] project is complete, as I love that style.
* Getting Started
Let’s curtail the [[https://www.rust-lang.org/en-US/install.html][standard installation]], as I’ve [[https://sourabhbajaj.com/mac-setup/Rust/][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=):
#+begin_src sh
  brew install rustup
#+end_src

Use the =rustup= to install the Rust compiler (=rustc=) and the Rust package manager (=cargo=):
#+begin_src sh
  rustup-init
#+end_src

To verify, run:
#+begin_src sh
  rustc --version
#+end_src

Let’s get the rest of the Rust packages for development installed:
#+begin_src sh
  cargo install rustfmt
#+end_src

Install the [[https://rust-analyzer.github.io/][rust-analyzer]] is an Rust LSP server implementation (see [[https://rust-analyzer.github.io/manual.html#rust-analyzer-language-server-binary][these installation instructions]]). For Mac, we’ll do:
#+begin_src sh
  brew install rust-analyzer
#+end_src
* Configuration
Let’s find the =rust= binaries:
#+begin_src emacs-lisp
  (add-to-list 'exec-path (file-name-concat (getenv "HOME") ".cargo/bin"))
#+end_src

The [[https://github.com/brotzeit/rustic][rustic project]] incorporates [[https://github.com/rust-lang/rust-mode][rust-mode]] and provides these features:
  - cargo popup
  - multiline error parsing
  - translation of ANSI control sequences through [[https://github.com/atomontage/xterm-color][xterm-color]]
  - async org babel
  - automatic LSP configuration with [[https://github.com/joaotavora/eglot][eglot]] or [[https://github.com/emacs-lsp/lsp-mode][lsp-mode]]
  - cask for testing

#+begin_src emacs-lisp
  (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))
#+end_src

** The Playground
The [[https://github.com/grafov/rust-playground][rust-playground]] project can create / cleanup rust scratch projects quickly:
#+begin_src emacs-lisp
  (use-package rust-playground)
#+end_src
   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 [[https://github.com/dryman/toml-mode.el][toml-mode]] adds syntax highlighting for [[https://github.com/mojombo/toml][toml files]].
#+begin_src emacs-lisp
  (use-package toml-mode)
#+end_src
* LSP
* Technical Artifacts                                :noexport:
Let's =provide= a name so we can =require= this file:

#+begin_src emacs-lisp :exports none
  (provide 'ha-programming-rust)
  ;;; ha-programming-rust.el ends here
  #+end_src

#+description: Emacs configuration for the Rust programming language.

#+property:    header-args:sh :tangle no
#+property:    header-args:emacs-lisp  :tangle yes
#+property:    header-args    :results none :eval no-export :comments no mkdirp yes

#+options:     num:nil toc:t todo:nil tasks:nil tags:nil date:nil
#+options:     skip:nil author:nil email:nil creator:nil timestamp:nil
#+infojs_opt:  view:nil toc:t ltoc:t mouse:underline buttons:0 path:http://orgmode.org/org-info.js