Difference between revisions of "Rust"

From WikEmacs
Jump to navigation Jump to search
(→‎Debugging: typos)
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
Let's setup Emacs for Rust development, shall we ?
 
Let's setup Emacs for Rust development, shall we ?
 +
 +
Also note that [https://spacemacs.org/layers/+lang/rust/README.html Spacemacs has a ready to use Rust layer]. If you wish other sanier defaults and configs, see [[Starter_Kits]].
  
 
= rust-mode =
 
= rust-mode =
Line 5: Line 7:
 
[https://github.com/rust-lang/rust-mode rust-mode] (in [[MELPA]]) offers:
 
[https://github.com/rust-lang/rust-mode rust-mode] (in [[MELPA]]) offers:
  
* syntax highlighted,  
+
* syntax highlighting,  
 
* '''code formatting''', optionally automatic,  
 
* '''code formatting''', optionally automatic,  
 
* [[Imenu]] support,  
 
* [[Imenu]] support,  
Line 17: Line 19:
 
[[File:rust-autocomplete.png|700px]]
 
[[File:rust-autocomplete.png|700px]]
  
== Code formatting with rustfmv ==
+
== Code formatting with rustfmt ==
  
 
Formatting is also provided by this mode. If you want auto formatting everytime you save a buffer, put this in your [[Init File]]:
 
Formatting is also provided by this mode. If you want auto formatting everytime you save a buffer, put this in your [[Init File]]:
Line 25: Line 27:
 
</code>
 
</code>
  
 +
= On-the-fly syntax checking: Flycheck-rust =
 +
 +
See [https://github.com/flycheck/flycheck-rust flycheck-rust] (on MELPA). See [[Flycheck]].
 +
 +
= Emacs-racer =
 +
 +
See [https://github.com/racer-rust/emacs-racer emacs-racer] (also in MELPA) for documentation and screencasts. It provides:
 +
 +
* code completion of variables, functions and modules (not macros yet due to a racer limitation) (see also [[company-mode]])
 +
* '''jump to definition''' (bound to '''M-.''')
 +
* '''describe functions and types''',
 +
 +
= Run Cargo tasks =
 +
 +
[https://github.com/kwrooijen/cargo.el cargo.el] ("cargo" in MELPA) gives us a set of key combinations to perform Cargo tasks within our Rust projects.
 +
 +
See also [[Compilation]].
 +
 +
= Debugging =
 +
 +
Use the built-in [[gdb]] and use '''rust-gdb''' to launch gdb with the Rust-specific pretty-printers enabled.
 +
 +
{{Note| set breakpoints with "break file:line" instead of "rbreak" which can give an "unmatched quote" error.}}
 +
 +
= Users snippets =
 +
 +
== Function to toggle a variable mutability ==
 +
 +
Given a line:
 +
 +
<pre>let foo = bar;</pre>
 +
 +
Calling this function with the cursor anywhere on that line will change it to
 +
 +
<pre>let mut foo = bar;</pre>
 +
 +
<source lang="lisp">
 +
(defun toggle-mut ()
 +
  "Toggles the mutability of the variable defined on the current line"
 +
  (interactive)
 +
  (save-excursion
 +
    (back-to-indentation)
 +
    (forward-word)
 +
    (if (string= " mut" (buffer-substring (point) (+ (point) 4)))
 +
        (delete-region (point) (+ (point) 4))
 +
      (insert " mut"))))
 +
</source>
 +
 +
To bind it to a key in rust-mode only:
  
 +
<source lang="lisp">
 +
(add-hook 'rust-mode-hook
 +
          (lambda ()
 +
            (local-set-key (kbd "C-M-m") #'toggle-mut)))
 +
</source>
 +
(source: [https://www.reddit.com/r/rust/comments/3le943/emacs_function_for_toggling_variable_mutability/ reddit]).
  
 
[[Category:Programming languages]]
 
[[Category:Programming languages]]
 
[[Category:Programming]]
 
[[Category:Programming]]

Latest revision as of 15:30, 22 May 2017

Let's setup Emacs for Rust development, shall we ?

Also note that Spacemacs has a ready to use Rust layer. If you wish other sanier defaults and configs, see Starter_Kits.

rust-mode

rust-mode (in MELPA) offers:

  • syntax highlighting,
  • code formatting, optionally automatic,
  • Imenu support,
  • navigation by semantics (go to the beginning or end of the current defun), (see also helm-swoop, helm-ag, tags,…)
  • playpen helpers (send the selected code to play.rust-lang.org),

In Debian testing and unstable you can also install it with apt install elpa-rust-mode (but the MELPA way is prefered).


700px

Code formatting with rustfmt

Formatting is also provided by this mode. If you want auto formatting everytime you save a buffer, put this in your Init File:

(setq rust-format-on-save t)

On-the-fly syntax checking: Flycheck-rust

See flycheck-rust (on MELPA). See Flycheck.

Emacs-racer

See emacs-racer (also in MELPA) for documentation and screencasts. It provides:

  • code completion of variables, functions and modules (not macros yet due to a racer limitation) (see also company-mode)
  • jump to definition (bound to M-.)
  • describe functions and types,

Run Cargo tasks

cargo.el ("cargo" in MELPA) gives us a set of key combinations to perform Cargo tasks within our Rust projects.

See also Compilation.

Debugging

Use the built-in gdb and use rust-gdb to launch gdb with the Rust-specific pretty-printers enabled.

Note: set breakpoints with "break file:line" instead of "rbreak" which can give an "unmatched quote" error.

Users snippets

Function to toggle a variable mutability

Given a line:

let foo = bar;

Calling this function with the cursor anywhere on that line will change it to

let mut foo = bar;
(defun toggle-mut ()
  "Toggles the mutability of the variable defined on the current line"
  (interactive)
  (save-excursion
    (back-to-indentation)
    (forward-word)
    (if (string= " mut" (buffer-substring (point) (+ (point) 4)))
        (delete-region (point) (+ (point) 4))
      (insert " mut"))))

To bind it to a key in rust-mode only:

(add-hook 'rust-mode-hook
          (lambda ()
            (local-set-key (kbd "C-M-m") #'toggle-mut)))

(source: reddit).