EmacsによるRust開発環境構築

概要

lsp-mode を使った開発環境に少し慣れてきたので、Rustの開発環境を構築してみます。

下記を参考にしました。

Rustプログラミングのための環境構築
日本のEmacsユーザーのためのハブサイト

Language server については、rls は既にarchiveとなり rust-analyzer に以降しているようなのでこちらを導入します。 導入方法も上記記載の方法ではできなかったので公式ページの手順で行いました。

rustup のインストール

rustup.rs - The Rust toolchain installer
The Rust toolchain installer

上記の通りに行います。

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /home/wurly/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /home/wurly/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /home/wurly/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /home/wurly/.profile
  /home/wurly/.bashrc

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

上記は 1 を選択します。

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2023-10-05, rust version 1.73.0 (cc66ad468 2023-10-03)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
info: installing component 'rust-std'
 24.7 MiB /  24.7 MiB (100 %)  17.1 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 61.6 MiB /  61.6 MiB (100 %)  19.4 MiB/s in  3s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.73.0 (cc66ad468 2023-10-03)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source "$HOME/.cargo/env"

現在のシェルで有効にするには source “$HOME/.cargo/env” を実行します。

source “$HOME/.cargo/env” は .bashrc に自動で記載されていました。

rust-analyer

GitHub - rust-lang/rust-analyzer: A Rust compiler front-end for IDEs
A Rust compiler front-end for IDEs. Contribute to rust-lang/rust-analyzer development by creating an account on GitHub.
User Manual
rust-analyzer: building a better Rust IDE

上記を参考に rust-analyzer をインストールしました。

curl -L https://github.com/rust-lang/rust-analyzer/releases/latest/download/rust-analyzer-x86_64-unknown-linux-gnu.gz | gunzip -c - > ~/bin/rust-analyzer
Chmod +x ~/bin/rust-analyzer

lsp-mode の設定

最低限、下記の設定で動作しました。

(use-package rust-mode
  :ensure t
  :custom rust-format-on-save t)

(use-package cargo
  :ensure t
  :hook (rust-mode . cargo-minor-mode))

(use-package lsp-mode
  :ensure t
  :hook (rust-mode . lsp)
  :custom (lsp-rust-server 'rust-analyzer)
  )

使い方

Cargo Minode mode (cargo) の説明には下記が記載されている。

Cargo Minor mode.
Provides a number of key combinations and functions for managing Cargo.
Current supported Cargo Key Combinations:
 * C-c C-c C-e - cargo-process-bench
 * C-c C-c C-b - cargo-process-build
 * C-c C-c C-l - cargo-process-clean
 * C-c C-c C-d - cargo-process-doc
 * C-c C-c C-v - cargo-process-doc-open
 * C-c C-c C-n - cargo-process-new
 * C-c C-c C-i - cargo-process-init
 * C-c C-c C-r - cargo-process-run
 * C-c C-c C-x - cargo-process-run-example
 * C-c C-c C-s - cargo-process-search
 * C-c C-c C-t - cargo-process-test
 * C-c C-c C-u - cargo-process-update
 * C-c C-c C-c - cargo-process-repeat
 * C-c C-c C-f - cargo-process-current-test
 * C-c C-c C-o - cargo-process-current-file-tests
 * C-c C-c C-O - cargo-process-outdated
 * C-c C-c C-m - cargo-process-fmt
 * C-c C-c C-k - cargo-process-check
 * C-c C-c C-K - cargo-process-clippy
 * C-c C-c C-a - cargo-process-add
 * C-c C-c C-D - cargo-process-rm
 * C-c C-c C-U - cargo-process-upgrade
 * C-c C-c C-A - cargo-process-audit
 * C-c C-c C-R - cargo-process-script
 * C-c C-c C-w - cargo-process-watch

下記でプログラムが実行できる。

C-c C-c C-r - cargo-process-run

また、下記に対し例えば Regex 等を指定すると、cargo add のパッケージ追加が行われます。

C-c C-c C-a - cargo-process-add
Copied title and URL