LoginSignup
1
1

More than 1 year has passed since last update.

Rustでのデバッグ環境構築(Windows+VisualStudioCode+rust-analyzer)

Last updated at Posted at 2022-07-04

概要

  • Windows環境+Visual Studio CodeでHello,Worldでデバッグをやる際に必要な設定等を記述しています。
  • 「Rust for Visual Studio Code」が非推奨となり「rust-analyzer」を使うようお勧めされるようになりました。

前提条件

  • ある程度Visual Studio Codeの扱いになれていること。

環境

  • Windows 10 Pro 21H2
  • rustc 1.62.0 (a8314ef7d 2022-06-27)
  • cargo 1.62.0 (a748cf5a3 2022-06-08)
  • Visual Studio Code 1.68.1

環境構築

Rustインストール

Visual Studio Build Tools 2019インストール

  • 「C++によるデスクトップ開発」をチェック→右下の「インストール」をクリック。
    • 「Visual Studio」の「C++によるデスクトップ開発」でも代替可能です。

Visual Studio Codeインストール

  • Visual Studio Codeからダウンロードページへ遷移。
  • 「Windows 8, 10, 11」をダウンロード。
  • VSCodeUserSetup-x64-1.68.1.exeを実行。(バージョン違ってても大丈夫だと思います。)
    • 初回起動時に言語パックをインストールするよう促されますのでそのままインストールしてください。

rust-analyzerインストール

  • 拡張機能:「rust-analyzer」をインストールする
    • コードアシスト、フォーマット等に使用します。

CodeLLDBインストール

  • 拡張機能:「CodeLLDB」をインストールする
    • デバッグ時に使用します。

基本的な操作

プロジェクト作成

  • PowerShellを開き以下を入力し実行する。
cargo new cargo_hello

プロジェクトを開く

  • 先程作成したプロジェクトフォルダを「Visual Studio Code」で開いてください。
    mainrs - hello_cargo - Visual Studio Code_2022-7-4_12-51-12_No-00.png

実行時の構成設定

  • メニュ→実行→構成の追加→「Cargo.toml has been detected in ...」→Yes

実行/デバッグ方法

実行例

  • 「デバッグなしで実行」を行う。

デバッグ例

  • 「println」行のブレイクポイントを有効にする。
    mainrs - hello_cargo - Visual Studio Code_2022-7-4_12-58-40_No-00.png
  • 「デバッグの開始」でブレイクポイントで止まるようになる。
    mainrs - hello_cargo - Visual Studio Code_2022-7-4_13-0-57_No-00.png

参考文献

つまずいたところ

  • 「Visual Studio Build Tools」いれずにコンパイルエラーが出た。
  • 「cargo」パッケージ管理を使わずにコンパイルしたソースをデバッグしようとしてはまった。
1
1
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
1
1