こんにちは,しまさん(@shimasan0x00)です.
今回はRustの開発環境をMacのVSCodeで構築したいと思います.
早速Hello Worldしにいきましょう.
環境
macOS Mojave(10.14)
VSCode 1.28.1
スポンサーリンク
VSCodeのインストール
![](https://i0.wp.com/murabitoleg.com/wp-content/uploads/2018/10/5882cab32d7afc4212f1486d1d2a481b-5.png?resize=2528%2C1212&ssl=1)
VSCodeはMicrosoftが開発したエディタです.
無料で使うことができます.
プラグインやテーマも豊富でカスタマイズ性が高いのが特徴です.
VSCodeをインストールしてない方は以下のリンクからダウンロードしてインストールをしてください.
スポンサーリンク
Rustのインストール
![](https://i0.wp.com/murabitoleg.com/wp-content/uploads/2018/10/76e2c847e4718b7bd7e1b515bc559d49-1.png?resize=2560%2C1272&ssl=1)
Rust自体のインストールはすごく簡単です.Homebrewを使うかコマンドを実行するだけです.
おすすめはRust公式で紹介されているコマンドです.
curl https://sh.rustup.rs -sSf | sh
途中に選択肢が出てきますが,defaultでOKです(1を選択).
![](https://i0.wp.com/murabitoleg.com/wp-content/uploads/2018/10/b62888958c99cd756b71e233f9ec1a25-1.png?resize=1640%2C1204&ssl=1)
Homebrewの場合は,
brew install rust
です.
次に設定を行っていきます.
source ~/.cargo/env
rustup toolchain install nightly
rustup component add --toolchain=nightly rust-analysis
rustup component add --toolchain=nightly rls-preview
rustup component add --toolchain=nightly rust-src
バージョンの確認は以下のコマンドで行います.
rustc -V
cargo -V
rustup -V
#rustc 1.30.0
#cargo 1.29.0
#rustup 1.14.0
アップデート
Terminalを開いて以下のコマンドを実行してください.
rustup update
VSCodeでのプラグインインストール
次はVSCodeでRustを使えるようにするためにプラグインをインストールしていきます.
「Shift+⌘+X」のショートカットキーで拡張機能のタブを開くか左側にある赤枠のアイコンを押してください.
![](https://i0.wp.com/murabitoleg.com/wp-content/uploads/2018/10/tex3-5.png?resize=1504%2C1064&ssl=1)
Rust(rls)のインストール
![](https://i0.wp.com/murabitoleg.com/wp-content/uploads/2018/10/33f406befc9d8d21eb74c1486ab75fc4-1.png?resize=2560%2C1418&ssl=1)
「Rust(rls)」と検索し,インストールをしましょう.
CodeLLDBのインストール
![](https://i0.wp.com/murabitoleg.com/wp-content/uploads/2018/10/8f65b36e85dc7261932ce234e3127b90-1.png?resize=2560%2C1418&ssl=1)
デバッカーのLLDBのプラグインをインストールしましょう.
「CodeLLDB」と検索し,インストールしましょう.
スポンサーリンク
Rustのデバック,実行
Terminalで,
cargo new --bin プロジェクト名
とするか適当なフォルダに「sample.rs」を作成しましょう.
そうするとRLSのインストールしますか?と聞かれるのでインストールしましょう.
![](https://i0.wp.com/murabitoleg.com/wp-content/uploads/2018/10/4f2280ca4748d3a8c27aa3b044bce585-1.png?resize=912%2C186&ssl=1)
sampleを以下に示しておきます.
fn main()
println!("Hello, world!");
}
では実際に実行するための設定をします.
F5か,虫を押し,デバックメニューから構成の追加を選択します.
![](https://i0.wp.com/murabitoleg.com/wp-content/uploads/2018/10/277f7f4c8d58cbcb921c07649aa4f89f-1.png?resize=1274%2C774&ssl=1)
そうしたら出てくる「LLDB」を選択しましょう.
![](https://i0.wp.com/murabitoleg.com/wp-content/uploads/2018/10/r2-1.png?resize=1506%2C427&ssl=1)
そうするとある程度の「launch.json」が生成されます.
あとはTerminalに出力が出るように変更するだけです.
以下の設定を加えるだけで完了です.
"terminal": "integrated”,
うまくいかない場合は.vscode以下にlaunch.jsonを作成し,以下のコードをコピペしましょう.
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'Rust'",
"cargo": {
"args": [
"build",
"--bin=Rust",
"--package=Rust"
],
"filter": {
"kind": "bin"
}
},
"terminal": "integrated",
"args": [],
"cwd": "${workspaceFolder}"
},
]
}
では,F5を押しましょう.
![](https://i0.wp.com/murabitoleg.com/wp-content/uploads/2018/10/r4-1.png?resize=772%2C214&ssl=1)
できたぞぉ〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜.
さいごに
今回はRustの環境をVSCode上で構築しました.
Rust自体のインストールは非常に簡単でスタートするのは簡単ですね!!!