Rust
Programming Language
Links

Rust is what C++ should have been,
had it not escaped Bell Labs prematurely

26 June 2018

Learn Rust

Of course, begin with resources on the Rust programming language website: https://rust-lang.org/

There is The Rust Language Reference and The Rust Language Grammar. Both continue as works-in-progress 3 years after 1.0-stable (released May 2015) with pull-requests or filing issues welcomed: reference or grammar.md, respectively.

Reference to the Rust Standard Library, run:

rustup doc --std

Similarly, for the de facto tutorial, TRPL (2nd Ed), run:

rustup doc --book

While there’s much to appreciate and respect about it, its style may seem verbose for some tastes.

Critique:

In technical writing, conventional practice introduces new material by directly stating the most novel concepts for the audience first, followed by elaboration and examples.

By contrast, TRPL 1st and 2nd Edition use a narrative flow typical of many bloggers, where there’s a build-up of anticipation followed by a reveal of substance. The single most useful sentence– or sometime phrase– within each section stating the actual idiom often gets lost within their storytelling style.

Bridging such differences: they would do well by highlighting language idioms in a sidebar (or just a bold font, inline) for each subsection. This would preserve integrity of their writing style while accommodating both a more diverse audience and readers returning to find just the one tidbit of interest.

For those who grew up with K&R C lovingly within reach, there is another book a bit closer, in that it states an idiom first and then its companion example or explanation:

Highly recommended book: Programming Rust

(No affiliation with the authors or publisher)

As of mid-2018:

Blandy’s & Orendorff’s Programming Rust is the closest to a K&R C book for the Rust programming language so far.

It’s concise. It begins with the key concepts. It’s organized suitably for the pragmatic software developer with a dev-ops mindset.

For immediate help, visit #rust-beginners on IRC. (That link will open a chat session in your web browser, the main caveat being that as an anonymous user, you cannot mute join/leave messages.)

Stay Current

This Week In Rust
Follow Rust programming language and community updates. Reading just this one is likely enough for most people, as they aggregate all of the other major resources.

Official blog
Consider reading from the run up to v1.0-stable, because tracking each update or new feature makes it cognitively easier to holistically comprehend the current version.

Announcements on The Rust Programming Language Forum

Likely to be covered on one of the above websites when it’s news, but complete collection of confirmed vulnerable exploits: RustSec advisory-db

Overcome & Persevere

Certain language features are challenging even for those who may believe they’ve grasped the core concepts.

Here are some resources found to be helpful.

Introduction to the mindset used by Rust:
Rust Means Never Having to Close a Socket from pre-1.0 Rust (Oct 2014) yet still relevant in mid-2018 for concepts explained.

Be sure to also read at least the introductory chapters of The Rustonomicon, even if you have no intention of ever using unsafe code. Explanations given there provide a more diverse perspective on ownership, borrowing, lifetimes, variance (Vlad’s answer may help explain concepts behind the jargon), drop check, etc.

Selection of forum posts, such as from Stack Overflow and Reddit:

A catalogue of Rust design patterns
Even for those coming from one of the Lisp languages/dialects and agree with Rich Hickey that patterns mean you’ve “run out of language,” this list is still quite valuable. (And yes, Rust has robust Macros that accommodate many of the use cases for which Common Lisp macros are actually used.)

Podcasts:

WebAssembly: (wasm)

Configure Emacs for Rust

For long-time users of Emacs, there’s a better way to load & configure packages. (Yes, that’s right, it’s actually changed since the 1980’s– who would have thought?)

This requires:

  1. Emacs package system
  2. use-package

For your ~/.emacs file or equivalent:

(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/"))
(add-to-list 'package-archives
             '("melpa-stable" . "https://stable.melpa.org/packages/"))
(setq package-archive-priorities '(("melpa" . 10) ("gnu" . 9))
      package-enable-at-startup nil
      use-package-always-ensure t)
(package-initialize)
(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
(eval-when-compile
  (require 'use-package)
  (use-package cl))

The Emacs implementation of the Language Server Protocol provides the very popular and useful feature from Visual Studio, which overlays relevant compiler messages and hints directly with the offending code.

See also eglot, an alternative and lighter LSP implementation than lsp-mode but requires Emacs-26. (e.g., Ubuntu 18.04 ships with Emacs-25.2.)

(use-package lsp-mode
  :config
  (setq lsp-ui-sideline-show-hover nil))

The default for lsp-ui-sideline-show-hover is non-NIL, which display information about symbols which can more distracting than useful.

NIL here doesn’t impact display of flycheck diagnostics or Code Actions.

For additional settings for lsp-mode above, use M-x describe-variable on each of these:

There is a separate package for the LSP UI:

(use-package lsp-ui
  :config
  (add-hook 'lsp-mode-hook #'lsp-ui-mode)
  :custom-face   ; see also: M-x list-colors-display
  (lsp-ui-sideline-current-symbol ((t :foreground "brown"
                                      :weight ultra-bold
                                      :box (:line-width -1 :color "brown")
                                      :height 0.99)))
  (lsp-ui-sideline-code-action ((t :foreground "orange"))))

The colour scheme above helps with Emacs displaying white background with black text; otherwise, some text was too faint to see.
(I keep brightness reduced to one click from off/black; i.e., rc.local has:
echo 50 > /sys/class/backlight/intel_backlight/brightness)

Hook for Rust language source code buffers to access build manager:

(use-package cargo
  :after rust-mode
  :config
  (add-hook 'rust-mode-hook #'cargo-minor-mode)
  (add-hook 'cargo-process-mode-hook #'visual-line-mode))

Ironically, there is apparently no file mode for rust’s Cargo.toml files:

(add-to-list 'auto-mode-alist '("Cargo.toml\\'" . conf-mode))

For TAB key for word-completion, used by racer:

(use-package company
  :config
  (add-hook 'racer-mode-hook #'company-mode))

Company also facilitates completion-at-point via C-M-i keys:

(use-package company-lsp
  :after company
  :config
  (setq company-lsp-enable-recompletion t)
  (lsp-define-stdio-client lsp-rust "rust" #'lsp-rust--get-root nil
                           :command-fn #'lsp-rust--rls-command
                           :initialize #'lsp-rust--initialize-client)
  (push 'company-lsp company-backends))

Provides M-. (meta-dot) for Rust source code buffers:

(use-package racer
  :after 'rust-mode
  :config
  (add-hook 'rust-mode-hook #'racer-mode))

Finally, rust-mode itself:

(use-package rust-mode
  :config
  ;; Requires the Language Server Protocol (LSP) be enabled,
  ;; but sometimes in emacs-25.2 fails with
  ;; "Suspicious state from syntax checker rust-cargo..."
  ;; When viewing rust code, use: M-x flycheck-buffer
  (add-hook 'rust-mode-hook #'flycheck-mode)
  (add-hook 'rust-mode-hook #'(lambda () (vc-mode-line nil)))
  (vc-mode-line nil)
  (setq cargo-process--command-clippy "+nightly clippy"
        company-tooltip-align-annotations t
        c-syntactic-indentation t
        c-basic-offset 2)
  :bind (("C-c C-c b" . cargo-process-build)
         ("C-c C-c c" . cargo-process-clippy)))

Not quite a REPL, Rust Playground is similar to *slime-scratch* as code sandbox. Start via: M-x rust-playground

For sharing like gist: M-x rust-playpen-region or rust-playpen-buffer

Try: M-x rust-playground-download

(If prompted for comment syntax, this is for Cargo.toml file, so use # 0x23 character.)

(use-package rust-playground
  :after rust-mode
  :config
  (setq rust-playground-basedir "/tmp/rust-playground"))

The default for Rust playground writes to ~/.emacs.d/rust-playground/ yet may be overridden by assigning rust-playground-basedir as above.

For LINT checking, consider installing Rust’s Clippy.

Run:

rustup install nightly && cargo +nightly install clippy

Then invoke with: M-x cargo-process-clippy or bind a key to that.

Links

From the official websites and repos:

Search packages, and read their documentation:

Forum, IRC Channels and more are linked from Community page.

Useful Tools

Rust Language Server (aka RLS) implements the Language Server Protocol (LSP) for Visual Studio, and it’s available within Emacs (see above) plus many other code editors.

Why you want this:

When your code editor supports LSP, you’ll get live annotations as an overlay with your code indicating programming errors of syntax, mismatched types, conflicting lifetimes, violations of API contracts, etc.

Other helpful messages are also provided from an instance of the compiler running in the background.

These messages are delivered in real-time and conventionally displayed inline or close to the offending line of code while you type it.

Helpful For Understanding

In no particular order:

Interesting Libraries

Unsorted Links

Recently plucked from the ‘net, offered without context or explanation:

This page is a work-in-progress.

Final thoughts:


         Since C++ started as an incremental upgrade to C,
                       Rust would be C *= 11
Copyright © 2018 Daniel Joseph Pezely
May be licensed via Creative Commons Attribution.