アクトインディ開発者ブログ

子供とお出かけ情報「いこーよ」を運営する、アクトインディ株式会社の開発者ブログです

rustup

こんにちは、tahara です。

この記事はアクトインディ Advent Calendar 2016 11日目です。

rustup: the Rust toolchain installer について書いてみたいと思います。

rustup は Rust における Ruby での rbenv や RVM みたいなものです。 以前は、multirust がありましたが、rustup 使いましょう、ということになっているようです。

rustup のインストールは簡単です。 https://www.rustup.rs/ に書いてあるとおりです。

curl https://sh.rustup.rs -sSf | sh

そうすると次のようきいてくるので、そのまま Enter でいいと思います。

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

これで rustc, cargo などが ~/.cargo/bin にインストールされます。 ~/.profile も編集され PATH~/.cargo/bin が追加されます。

そして最後に表示されるメッセージ

To configure your current shell run source $HOME/.cargo/env.

をシェルから実行すると、rustc が使えるようになります。

~$ source $HOME/.cargo/env
~$ rustc --version
rustc 1.13.0 (2c6933acc 2016-11-07)

処理系インストールできたら、とりあえず

~$ vi /tmp/hello.rs
fn main() {
    println!("Hello, world!");
}
~$ rustc /tmp/hello.rs
~$ ./hello
Hello, world!

あとはおまけで Emacs まわりの設定を簡単に書きておきます。

cargo を使ってコード整形のための rustfmt とコード保管のための racer をインストールします

cargo install rustfmt
cargo install racer

~/.emacs は次の感じで。flycheck は好きでないので外してあります。

(unless (package-installed-p 'rust-mode)
  (package-refresh-contents)
  (package-install 'rust-mode)
  (package-install 'cargo)
  ;;(package-install 'flycheck-rust)
  (package-install 'racer)
  (package-install 'company)
  (package-install 'rustfmt))
(add-hook 'rust-mode-hook #'racer-mode)
(add-hook 'rust-mode-hook #'rustfmt-enable-on-save)
(add-hook 'racer-mode-hook #'eldoc-mode)
(add-hook 'racer-mode-hook #'company-mode)
;;(add-hook 'after-init-hook #'global-flycheck-mode)
;;(add-hook 'flycheck-mode-hook #'flycheck-rust-setup)
;;(global-set-key (kbd "TAB") #'company-indent-or-complete-common)
(setq company-tooltip-align-annotations t)
;; Reduce the time after which the company auto completion popup opens
(setq company-idle-delay 0.2)
;; Reduce the number of characters before company kicks in
(setq company-minimum-prefix-length 1)

以上になります。

弊社でもごく一部の社内ツールに Rust を使い始めました。 来年も Rust がよりいっそう盛り上がるといいですね!