Rust on Ubuntu 20.04 in WSL2

はじめに_

WSL2上にUbuntuがインストールされているとする。

参考_

以下の記事に基づきインストールする。

Rustのインストール_

コンパイルに必要なソフトウェアをインストールする→build-essentialとは

% sudo apt install -y build-essential

Rust公式:はじめにに従いインストールする。

% curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
~中略~
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

% which cargo
/home/gotoh/.cargo/bin/cargo

% which rustc
/home/gotoh/.cargo/bin/rustc

% rustc --version
rustc 1.52.1 (9bc8c42bb 2021-05-09)

動作確認

% mkdir -p ~/Sandbox/Rust
% cd ~/Sandbox/Rust
% cargo new --bin prj_evid
     Created binary (application) `prj_evid` package
% cd prj_evid/

% ls -1F
Cargo.toml
src/

VSCodeの設定_

VSCodeのインストール_

以下に従いインストールする。

拡張機能の設定_

VSCodeが起動中の場合は一度終了し、Ubuntu側から起動する。

% code .

拡張「Rust(rust-lang.rust)」インストールに従い、拡張機能Rustをインストールする。

  1. 拡張機能Rustをインストール
  2. 先ほど作った prj_evid/src/main.rs をVSCodeで開く
  3. 「Some Rust components not installed.Install ?」のポップアップが出たら「Yes」を押す

拡張機能CodeLLDBをインストールする。Qiita:@84zume Visual Studio CodeでRust開発環境を整えるの「CodeLLDBのセットアップ」を参考に使用する。

  1. 拡張機能CodeLLDBをインストール
  2. 先ほど作った prj_evid/src/main.rs をVSCodeで開く
  3. 「実行」→「デバッグ開始」を選択する
  4. エラーメッセージがウィンドウで表示されるので、OKを押して進める。

戻る_