Rustの環境構築をする( vim, zsh )
dotfilesを整備している最中、ついうっかりrustを触ってみようと魔がさしました。 環境を整えつつイントロダクションを読み進め、最低限触れる状態を構築します。
本記事は、あくまでプログラミング経験者がrustの環境構築を行うものです。 詳細度が高くより知識を深めるような特性は持っていないと考えているので、これから業務等で扱うという方は別の記事を参照された方が良いかもしれません。
Install Rust / インストール
RustのInstallationページを見ると、詳細に書かれています。
The first step is to install Rust. We’ll download Rust through rustup, a command line tool for managing Rust versions and associated tools.
Rust
は、Rust
自体のバージョンと関連するツールを管理するコマンドラインツールである rustup
からインストールします。
Install rustup
If you prefer not to use rustup for some reason, please see the Rust installation page for other options.
なんらかの理由で rustup
を入れたくない限りは入れましょう。
MacOS or Linuxであれば、以下のコマンドで入れることができます。
$ curl https://sh.rustup.rs -sSf | sh
途中でオプションについて質問がありますが、特にこだわりがなければデフォルトで良いです。
Current installation options: default host triple: x86_64-apple-darwin default toolchain: stable modify PATH variable: yes 1) Proceed with installation (default) 2) Customize installation 3) Cancel installation
なお、 curlが使えない場所にいるあなたは、ただちに使える場所へ移動してください。
インストールが終わると、褒められます。
Rust is installed now. Great!
Pathを通す
以下のコマンドでホームディレクトリ直下の .cargo/env
を読みに行きます。
するとPATHに $HOME/.cargo/bin
がexportされます。
$ source $HOME/.cargo/env ... # envの中身 export PATH="$HOME/.cargo/bin:$PATH"
継続して開発をするのであれば、 .bashrcや.zshrcなどにパスを通す処理を追加してください。
echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> <任意のファイル>
Windowsの人はドキュメント読んで頑張ってください。
Update rustup / 更新
最後にrustupの更新をします。
$ rustup update
HelloWorld
rust向けにディレクトリを作成し、そこに main.rs
ファイルを作成します。
~/src/github.com/paveg pav@ryota.local ❯❯❯ mkdir rust_sample pav@ryota.local ❯❯❯ cd rust_sample ~/src/github.com/paveg/rust_sample pav@ryota.local ❯❯❯ touch main.rs pav@ryota.local ❯❯❯ vi main.rs
main.rs
fn main() { println!("Hello World!"); }
実行
rustcでcompileを行ってからバイナリを実行します。
pav@ryota.local ❯❯❯ rustc main.rs ~/src/github.com/paveg/rust_sample pav@ryota.local ❯❯❯ ./main Hello World!
vim-quickrun
vim-pluginである vim-quickrunをインストールしていれば、そのまま実行が可能です。
GitHub - thinca/vim-quickrun: Run commands quickly.
vim/dotfilesの設定
追記したものだけ明記します。
具体的にはシンタックスハイライトの挿入と、環境変数の追加のみです。
_rustup
は端末ごとに個別に実行してほしいのでここではignoreしています。
Rust by paveg · Pull Request #50 · paveg/dotfiles · GitHub
ついでに LSP向けに rustup component add rls rust-analysis rust-src
を実行しておきます。
cargo
cargoはビルドシステム兼パッケージマネージャです。 新しくプロジェクトを開始する際は、cargoを使うと便利です。
~/src/github.com/paveg 31s pav@ryota.local ❯❯❯ cargo new sample_cargo Created binary (application) `sample_cargo` package pav@ryota.local ❯❯❯ cd sample_cargo/src
cargoによる設定ファイルの出力
~/src/github.com/paveg/sample_cargo/src pav@ryota.local ❯❯❯ cat ../Cargo.toml ──────────────── File: ../Cargo.toml ──────────────── [package] name = "sample_cargo" version = "0.1.0" authors = ["Ryota Ikezawa <pavegy@gmail.com>"] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] ──────────────── ~/src/github.com/paveg/sample_cargo/src pav@ryota.local ❯❯❯ cat ../Cargo.lock ──────────────── File: ../Cargo.lock ──────────────── # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] name = "sample_cargo" version = "0.1.0" ────────────────
おわりに
LSP導入したvimは以下のような感じです。