無限大な夢のあと

テニスとアニメが大好きな厨二病SEのブログ

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

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

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

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

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

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

王様達のヴァイキング 11 (ビッグコミックス)

王様達のヴァイキング 11 (ビッグコミックス)

matome.naver.jp

また、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を使用する。
    1. ctagsをインストール。

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


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

      cargo install rusty-tags



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

    cargo install cargo-edit


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

    cargo new hello_world --bin


  15. Rustのコードを実行。
    1. コマンドライン

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


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


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


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

    はじめる

    • atomの導入について参考にさせて頂きました。

    qiita.com

    Scalaスケーラブルプログラミング第3版

    Scalaスケーラブルプログラミング第3版

    Programming Rust: Fast, Safe Systems Development

    Programming Rust: Fast, Safe Systems Development

    Atom実践入門──進化し続けるハッカブルなエディタ (WEB+DB PRESS plus)

    Atom実践入門──進化し続けるハッカブルなエディタ (WEB+DB PRESS plus)

    テキストエディタAtom入門 (OIAX BOOKS)

    テキストエディタAtom入門 (OIAX BOOKS)