冥冥乃志

ソフトウェア開発会社でチームマネージャをしているエンジニアの雑記。アウトプットは少なめです。

follow us in feedly

Rustの開発環境を作る(その1:IntelliJのプラグインを入れるまで)

先日、気になる技術をピックアップしてみましたが、その中からRustをやってみることにしました。

まずは、Hello World的な簡単なプログラムを書いて、コンパイルして動かす環境を作ってみます。VimやらEmacsやら特定の宗教に帰依していないので、できればIntelliJでコードが書けるように環境が作れればいいな、と。

コンパイラのインストール

何はともあれコンパイラが必要です。公式サイトのトップに各環境向けのコンパイラのダウンロードリンク用意されています。ダウンロードして実行するだけです。

www.rust-lang.org

インストール設定がデフォルトから変更がなければ /usr/local/bin/rustc がインストールされてパスが通っているはずです。

コマンドラインでのコンパイル

公式サイトのトップに出ているサンプルを写経して実行してみます。

mao 1ststep $ rustc myfirstrust.rs

myfirstrust.rs:15:18: 15:19 error: unknown start of token: \\
myfirstrust.rs:15   pringln!("\"{}"\の計算結果は{}です。", program, accumurator);

タイポしましたテヘペロ 。コマンドのタイポの時は unknown start of token とメッセージが出ることがわかりました。

mao 1ststep $ rustc myfirstrust.rs

myfirstrust.rs:15:42: 15:53 error: unresolved name `accumurator`. Did you mean `accumulator`? [E0425]
myfirstrust.rs:15   println!("\"{}\"の計算結果は{}です。", program, accumurator);

もう一個のタイポにも気づかずorz。定義がファイル内にあるからなのか、accumlatorじゃないの?というサジェストもしてくれました。サンプルが小さいので、どこまでサジェストしてくれるかはまだわかりません。

改めて、コンパイル

mao 1ststep $ rustc myfirstrust.rs
error: linking with `cc` failed: exit code: 69
note: "cc" "-m64" "-L" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib" "myfirstrust.0.o" "-o" "myfirstrust" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib" "-L" "/Users/mao/Documents/RustWorkspace/1ststep/.rust/lib/x86_64-apple-darwin" "-L" "/Users/mao/Documents/RustWorkspace/1ststep/lib/x86_64-apple-darwin" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libstd-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libcollections-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/librustc_unicode-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/librand-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liballoc-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liballoc_jemalloc-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liblibc-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libcore-35c36e89.rlib" "-l" "System" "-l" "pthread" "-l" "c" "-l" "m" "-l" "compiler-rt"
note:

Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.

error: aborting due to previous error

mao 1ststep $ sudo rustc myfirstrust.rs
Password:
error: linking with `cc` failed: exit code: 69
note: "cc" "-m64" "-L" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib" "myfirstrust.0.o" "-o" "myfirstrust" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib" "-L" "/Users/mao/Documents/RustWorkspace/1ststep/.rust/lib/x86_64-apple-darwin" "-L" "/Users/mao/Documents/RustWorkspace/1ststep/lib/x86_64-apple-darwin" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libstd-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libcollections-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/librustc_unicode-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/librand-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liballoc-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liballoc_jemalloc-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liblibc-35c36e89.rlib" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libcore-35c36e89.rlib" "-l" "System" "-l" "pthread" "-l" "c" "-l" "m" "-l" "compiler-rt"
note:
You have not agreed to the Xcode license agreements, please run 'xcodebuild -license' (for user-level acceptance) or 'sudo xcodebuild -license' (for system-wide acceptance) from within a Terminal window to review and agree to the Xcode license agreements.

どうやらXcodeライセンスの承諾がされていない模様。El CapitanにあげてからXCode使ってなかったしそういうことかな、と。 sudo xcodebuild -license を実行して再度コンパイルしたらうまくいきました。ちなみに、このサンプルの実行ファイルの大きさは267kバイトです。

実行可能バイナリを実行してみましょう。

mao 1ststep $ ./myfirstrust
"+ + * - /"の計算結果は1です。

プログラムが短いとはいえ、コンパイルも実行もIntelliJ 15が未対応の古いマシンで一瞬だったのでネイティブの環境は強いな、と思った次第でございます。

IDEで書く

IDEサポートに関する情報は、公式に出ています。

Rust and IDEs

上記読むと、RustチームとしてはIDEのサポートは重要なものだと考えているから、公式で対応してるところだよ、ということみたいですね。IntelliJEclipseVisual Studio向けにIDEプラグインを提供する計画で、EclipseVisual Studioは提供してるけど、IntelliJはまだなので自分でビルドしてくださいとのことです。

github.com

リポジトリの指示に従ってビルドします。依存関係のあるライブラリのダウンロードに時間がかかるので見守っておきましょう。

ドキュメントをパラ見しておきましょう。

github.com

ザクッとまとめるとこんな感じ。

  • cargoというパッケージマネージャがあるらしいが、まだ対応してないのでシェルからやってね
  • 頭の良いコード補完はないよ、ソースの中のものしか補完できません
  • ベーシックなフォーマットしかないから、再フォーマットは意図と違うかもね

つまり、ファイル内のコード補完とコードハイライトくらいしかない。他は全部シェルでやってね、とのことらしいです。実際、プロジェクトの設定もビルド・実行設定もRust用はありません。コンパイルと実行もシェルからやらないといけませんでした。

パッケージマネージャがないとやってられない、というレベルのようですね。Cargoというパッケージマネージャがデフォルトのようなので、それを合わせて使ってみようと思います。以下次号。