Rust の開発環境セットアップ
Linux (Ubuntu 19.10) に Rust の開発環境を作ったメモです。IDE として VSCode を使います。
Rust の開発環境
- Rust ツールチェイン
- リンカ
- コンパイル済みのオブジェクトファイルやライブラリをリンクして実行可能バイナリにするツール
- OS ごとに標準的に用意されているもの (
ldなど) を使う
- rustup
- Rust ツールチェインや関連ツールのインストール・管理ツール
- 複数のバージョン (stable, beta, nightly) のツールチェインを管理できる
- rbenv, pyenv のようなイメージ
- クロスコンパイル用のターゲットをインストールできる
開発環境のセットアップ
Rust ツールチェインと VSCode のセットアップを行う。
Rust ツールチェインのインストール
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer
Welcome to Rust!
This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.
It will add the cargo, rustc, rustup and other commands to
Cargo's bin directory, located at:
/home/takuya-a/.cargo/bin
This can be modified with the CARGO_HOME environment variable.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
/home/takuya-a/.rustup
This can be modified with the RUSTUP_HOME environment variable.
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/home/takuya-a/.profile
/home/takuya-a/.bash_profile
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
default host triple: x86_64-unknown-linux-gnu
default toolchain: stable
profile: default
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>
1 を入力してインストール。
~/.cargo/bin/にcargo,rustc,rustupなどがインストールされるCARGO_HOMEで変更できる
/.rustup/にrustupのメタデータとツールチェインがインストールされるRUSTUP_HOMEで変更できる
また、 ~/.profile と ~/.bash_profile に以下が追記される:
export PATH="$HOME/.cargo/bin:$PATH"
パスの設定をシェルに反映すると、 rustc , cargo , rustup などがそのまま使えるようになる。
$ . ~/.cargo/env
VSCode のセットアップ
- Rust (rls) 拡張 のインストール
- RLS (Rust Language Server) と通信して補完などを支援してくれる拡張
- 拡張をインストールしたあと、 RLS をインストールするかどうかをポップアップで聞かれるのでインストールする
- CodeLLDB 拡張 のインストール
プロジェクトの作成・ビルド・実行
以下で ~/workspace/hello/ 以下に bin クレート(バイナリパッケージ向けのプロジェクト)が作成される:
$ cd ~/workspace
$ cargo new --bin hello
Created binary (application) `hello` package
cargo build でパッケージをビルドできる:
$ cd hello
$ cargo build
Compiling hello v0.1.0 (/home/takuya-a/workspace/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.23s
ビルドした実行可能バイナリは target/debug/ 以下に置かれる:
$ ls target/debug/ build deps examples hello hello.d incremental $ target/debug/hello Hello, world!
参考
- 実践 Rust 入門 (2 章)
![実践Rust入門[言語仕様から開発手法まで] 実践Rust入門[言語仕様から開発手法まで]](https://images-fe.ssl-images-amazon.com/images/I/51e5B1Zx%2ByL._SL160_.jpg)