LoginSignup
68
68

More than 3 years have passed since last update.

Visual Studio CodeでRust開発環境を整える

Last updated at Posted at 2020-09-19

普段よく使っているエディタがマイクロソフトのVisual Studio Codeです。最近Rustの勉強を始めたので、Visual Studio Code上でRust環境を整えてみました。その際のセットアップ方法をまとめておこうと思います。Windows環境前提の記事です。

前提となる環境の情報

  • Windows ver.1903
  • Visual Studio Code 1.49.1
  • Rust 1.46.0

1. セットアップ

Rustは既にインストール済みという前提でスタートします。
(難しい手順ではないですが、いちおうこちらに手順をまとめました→Rustに入門してみた

今回導入するのは次の2つの拡張機能です。

拡張機能 名前とリンク 用途
image.png Rust for Visual Studio Code コードの補完、フォーマット、リファクタリング、ナビゲーションなど、コーディングに必要な便利ツールが一通りそろっています。
image.png CodeLLDB LLDBとは次世代の高機能デバッガーです。この拡張機能を利用することにより、Visual Studio CodeでRustのデバッグ実行を実施することができます。
image.png Better TOML 設定ファイルでTOMLを扱うために、編集支援用に使う拡張機能です。

拡張機能のインストールは特に難しいことはありません。Rust for Visual Studio Codeもインストールしちゃえば機能するので、説明は不要かと思います。CodeLLDBは設定があるので以下で補足します。

CodeLLDBのセットアップ

順を追って説明します。

  1. Rustのプログラムが格納されたフォルダをVisual Studio Codeで開く。
  2. 左のサイドバーからデバッグ実行を選択する。
  3. 「launch.jsonファイルを作成します。」をクリックし、設定ファイルであるlaunch.json ファイルを作成する

Inked1_LI.jpg

  1. コマンドパレットが開くため、デバッガーとして、LLDB を選択する

Inked2_LI.jpg

  1. 同じフォルダにあるCargo.tomlに基づいて、launch.jsonが自動生成される。ダイアログは「Yes」をクリック。

3.png

  1. これで設定完了!自動生成されたファイルは以下のイメージ。
launch.json
{
    // IntelliSense を使用して利用可能な属性を学べます。
    // 既存の属性の説明をホバーして表示します。
    // 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'hello'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=hello",
                    "--package=hello"
                ],
                "filter": {
                    "name": "hello",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug unit tests in executable 'hello'",
            "cargo": {
                "args": [
                    "test",
                    "--no-run",
                    "--bin=hello",
                    "--package=hello"
                ],
                "filter": {
                    "name": "hello",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

デバッグ実行してみる

デバッグ実行している際の画面イメージは以下のとおりです。

  • ブレークポイントで止められる
  • 逐次実行できる
  • ウォッチ式も定義できる
  • コールスタックも丸わかり

image.png

いい感じですね。

2.おわりに

これだけあれば、あとはさくさくRustのお勉強を進められそうです。誰かの参考になれば。

68
68
1

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
68
68