LoginSignup
7
6

More than 5 years have passed since last update.

UbuntuのVSCodeでRustの開発環境を作る

Last updated at Posted at 2019-04-14

UbuntuのVSCodeでRustの開発環境を構築したのでメモとして残しておきます:smile:

環境

  • ubuntu 18.04
  • vscode 1.33.1
  • rust 1.34.0

VSCodeの拡張機能

VSCodeにはRustのソースコードの補完や定義ジャンプ、整形、ビルドタスクなどの機能を持ったプラグインがあるのでそれをインストールします。マーケットプレイスでrustで検索するとRust(rlt)と出てくるのでそれをインストールします。

rust-rls.JPG

コマンドライン上でコンポーネントをインストールする方法

rustup component listコマンドで現在インストールされているコンポーネントを調べるとdefaultでインストールされているモノのみです。

~$ rustup component list | grep -e installed -e default
cargo-x86_64-unknown-linux-gnu (default)
rust-docs-x86_64-unknown-linux-gnu (default)
rust-std-x86_64-unknown-linux-gnu (default)
rustc-x86_64-unknown-linux-gnu (default)

VSCode上でインストールするものと同じコンポーネントをrustup component addコマンドでインストールします。

~$ rustup component add rust-analysis --toolchain stable-x86_64-unknown-linux-gnu
~$ rustup component add rust-src --toolchain stable-x86_64-unknown-linux-gnu
~$ rustup component add rls --toolchain stable-x86_64-unknown-linux-gnu

先ほどインストールしたコンポーネントが追加されています。

~$ rustup component list | grep -e installed -e default
cargo-x86_64-unknown-linux-gnu (default)
rls-x86_64-unknown-linux-gnu (installed)
rust-analysis-x86_64-unknown-linux-gnu (installed)
rust-docs-x86_64-unknown-linux-gnu (default)
rust-src (installed)
rust-std-x86_64-unknown-linux-gnu (default)
rustc-x86_64-unknown-linux-gnu (default)

VSCode上でコンポーネントをインストールする方法

Rust(rls)をインストールした後、ターミナルでcargo new helloとすると、RLS not installed. install?と聞かれるのでYesと答えてインストールします。

rust-rls-notinstalled.JPG

TERMINALに表示されるインストールログを見るとrustup component addコマンドでインストールする方法と同じコマンドを使っています。

rust-rls_install.JPG

動作確認

Vscodeを再起動して先ほど作成したhelloプロジェクト開きます。そしてターミナルでcargo rをするとコンパイル、実行と進みHello, world!と表示されています。

rust-run.JPG

ビルド時に"error: linker `cc` not found"というエラーがでたらbuild-essentialパッケージをインストールします。

~$ apt install build-essential

デバッグ環境

デバッガlldbをインストールします。

~$ sudo apt install lldb
~$ lldb --version
lldb version 6.0.0

vscode用のlldbフロントエンドCodeLLDBをインストールします。拡張機能マーケットプレイスでCodeLLDBで検索すると出てくるのでインストールします。

rust-debug.JPG

プロジェクトを開いている状態で、Debug-->Start Debuggingと押すかF5でデバッグ画面に映ります。

rust-debug2.JPG

初めてデバッガを起動するときに下のようなメッセージが出ると思いますがYesを押します。.vscodeディレクトリの下にlaunch.jsonが作成されます。launch.jsonはデバッガ構成の設定を行っています。

rust-debug-error.JPG

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6