LoginSignup
22
19

More than 5 years have passed since last update.

Rust入門 #0 Macで開発環境構築 (2017/1/9時点)

Last updated at Posted at 2017-01-07

※1/9に以下の対応を追加。

  • メソッドジャンプをする対応方法をrustyを使う方法に変更。
  • cargo-editをインストールする方法を追記。

皆様、あけましておめでとうございます。
2017年、よろしくお願い致します。

今年の一発目のブログはRustの開発環境構築です。
Rust自体は前職の方からも名前だけは聞いていたのですが、以下のちょっとしたきっかけで入門してみようと思いました。

  • 社内のIoTチャレンジ部で、ラズベリーパイで遊んでいた時に、省メモリでかつ関数型っぽく記述できると話題に上がったこと。
  • 書評レビューア募集で、Android開発の書籍を頂いたけど、 そのサンプルを新しい言語で動かしてみたいなと思ってみたこと。
  • 王様達のバイキングの11巻で、ValkyrjaというクラッカーがScalaやRustでプログラム書いていたという話が出てきたこと。(←これが一番の理由w)

また、2017/1/2時点でIntelliJのRustプラグインを用いても、メソッドのコード補完などもできないため、コード補完プラグインが存在するatomを用いることとします。

Macのスペック

  • マシン: MacBookPro (Retina,15-inch, Mid 2015)
  • OS: OS X EI Capitan 10.11.6
  • プロセッサ: 2.2 GHz Intel Core i7
  • メモリ: 16GB 1600 MHz DDR3

atom

  • バージョン 1.12.7
  • 日本語化対応済み

1.Rustをインストール。選択肢では、1を選択。

curl https://sh.rustup.rs -sSf | sh
This path will then be added to your PATH environment variable by modifying the
profile file located at:

  /Users/ユーザ名/.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-apple-darwin
     default toolchain: stable
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation

2.今回の手順では、.bashrcにPATHの記載を追加していく。
ただし、Mac OS Xのデフォルトでは、HOMEに.bashrcを作成してもターミナル起動時に自動で読み込むようにはなっていないようなので、自動で読み込むように.bash_profileに以下の記述を追加。

atom ~/.bash_profile
if [ -f ~/.bashrc ] ; then
. ~/.bashrc
fi

3.RustのパッケージマネージャーであるCargoのパスを通すために、~/.bashrcファイルに設定を追加。

atom ~/.bashrc
#Rust 
export PATH="$HOME/.cargo/bin:$PATH"

4.bashrcの設定を反映する。

source ~/.bashrc

5.rustc、cargo、rustupがインストールされたことを確認。

rustc --version
rustc 1.14.0 (e8a012324 2016-12-16)

cargo --version
cargo 0.15.0-nightly (298a012 2016-12-20)

rustup --version
rustup 1.0.0 (17b6d21 2016-12-15)

6.補完に必要なパッケージをインストールする。

cargo install racer

7.cargo .tomlを操作するのに便利なパッケージをインストールする。

cargo install cargo-edit

8.atomにRust用のプラグインとして、下記をインストールする。
※atom自体のインストールは省略。

apm install language-rust
apm install racer
apm install linter
apm install linter-rust
apm install atom-ctags
apm install script

9.racerというRustのコード補完を行うプラグインで必要なため、Rustのソースコードを落とす。

rustup component add rust-src

10.atomを起動し、環境設定>Packagesで、racerの設定を開く。

11.下記の設定項目を入力する。※フルパスでないと動作しなかった。

Path to the Racer executable

/Users/ユーザ名/.cargo/bin/racer

Path to the Rust source code directory

/Users/ユーザ名/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/src

12.メソッドジャンプを可能にするためにrusty-tagsを使用する。

12-1.ctagsをインストール。

brew tap universal-ctags/universal-ctags
brew install --HEAD universal-ctags

12-2.rusty-tagsをインストール。

cargo install rusty-tags

13.パッケージ管理を容易にできるcargo-editをインストール。

cargo install cargo-edit

14.動作確認として、プロジェクト作成する。

cargo new hello_world --bin

15.Rustのコードを実行。

15-1.コマンドライン

cd hello_world/
cargo run #Cargo.tomlのあるディレクトリで実行

15-2.atom
atomでファイルを開いている状態で、scriptプラグインのショートカット(Command+i)で実行する。

これでRustの開発環境構築ができて、書き始められるはず!

今回、参考にさせて頂いた公式チュートリアル/ブログ。

22
19
2

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
22
19