Rust 環境構築 + Hello, World + Cargo 基本

安全性、並列性、速度の3つにフォーカスしたシステムプログラミング言語である Rust に興味を持ったので、 まずは環境構築から始めてみます。

Rust 環境構築

  • Rust コンパイラは様々なプラットフォームをサポートしていますが、サポートのレベルが3つに分けられています。

    • 第1級: ビルドでき、かつ動作を保証する
    • 第2級: ビルドを保証する
    • 第3級: テストやビルドは保証していない
  • この中で 64-bit OSX (10.7+, Lion+) は第1級サポートに位置付けられています。

Mac OS での Rust 環境構築

  • 筆者の環境は以下の通りです。
$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14
BuildVersion:   18A391
  • 環境構築は以下のワンコマンドですみます。
$ curl https://sh.rustup.rs -sSf | sh

info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust programming
language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to Cargo's bin
directory, located at:

  /Users/htamakos/.cargo/bin

This path will then be added to your PATH environment variable by modifying the
profile files located at:

  /Users/htamakos/.profile
  /Users/htamakos/.zprofile

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
>1

info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: latest update on 2019-01-17, rust version 1.32.0 (9fda7c223 2019-01-16)
info: downloading component 'rustc'
64.5 MiB /  64.5 MiB (100 %)   4.5 MiB/s ETA:   0 s
info: downloading component 'rust-std'
 49.2 MiB /  49.2 MiB (100 %)   6.1 MiB/s ETA:   0 s
info: downloading component 'cargo'
info: downloading component 'rust-docs'
  8.5 MiB /   8.5 MiB (100 %)   7.6 MiB/s ETA:   0 s
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'
info: default toolchain set to 'stable'

  stable installed - rustc 1.32.0 (9fda7c223 2019-01-16)


Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done automatically.

To configure your current shell run source $HOME/.cargo/env
  • この後 $HOME/.cargo/bin を PATHに追加するか source $HOME/.cargo/env で rust 関連のコマンドが実行できます。2019/2時点で最新の安定バージョンは 1.32.0のようです。
$ source $HOME/.cargo/env
$ rustc --version                                                                                                                                             
rustc 1.32.0 (9fda7c223 2019-01-16)

Hello, Rust!

  • 何はともあれ Rust で Hello, World してみます。関数定義を fn で、標準出力をprintln関数で実行する、行末コロンがある、等の特徴がわかります。
fn main(){
  println!("Hello, World");
}
$ rustc main.rs
$ ls -lah
total 560
drwxr-xr-x  4 htamakos  staff   128B  2 15 00:35 .
drwxr-xr-x  3 htamakos  staff    96B  2 15 00:25 ..
-rwxr-xr-x  1 htamakos  staff   274K  2 15 00:35 main
-rw-r--r--  1 htamakos  staff    41B  2 15 00:34 main.rs

$ ./main
Hello, World

できた!

Hello, World 解説

  • C言語に近いシンタックス

    • {} で関数ブロックを記述
    • 行末にコロンが必須
    • main 関数は特別扱いで、すべての rust プログラムのエントリーポイントになる
  • println! はRustのマクロ呼び出し

    • println は関数で、それに対して!をつけるとマクロ呼び出しになるらしい

Cargo Introduction

  • Rust のビルドシステム兼パッケージマネージャーが Cargoです。
  • 上記のインストール手順を踏むと勝手にインストールされています。
$ cargo --version
cargo 1.32.0 (8610973aa 2019-01-02)
  • cargo でプロジェクトを作成するには以下のようなコマンドを実行します。
$ cargo new hello_rust --bin
  • するとnewの後に指定した名前のディレクトリが作成され、その下に、Cargo.tomlファイルと src/main.rs が作成されます。ついでにgitリポジトリの初期化も行ってくれるようです。
$ ls -lah hello_rust
total 16
drwxr-xr-x  6 htamakos  staff   192B  2 15 00:48 .
drwxr-xr-x  5 htamakos  staff   160B  2 15 00:48 ..
drwxr-xr-x  9 htamakos  staff   288B  2 15 00:51 .git
-rw-r--r--  1 htamakos  staff    19B  2 15 00:48 .gitignore
-rw-r--r--  1 htamakos  staff   125B  2 15 00:48 Cargo.toml
drwxr-xr-x  3 htamakos  staff    96B  2 15 00:48 src
  • Cargo.toml の中身は以下のようになっています。TOMLファイル形式が設定ファイルのフォーマットとして採用されているようです。
$ cat Cargo.toml
[package]
name = "hello_rust"
version = "0.1.0"
authors = ["htamakos <xxxxxx@gmail.com>"]
edition = "2018"

[dependencies]
  • Cargo で build する
$ cargo build
   Compiling hello_rust v0.1.0 (/Users/htamakos/project/learning_rust/01_hello_world/hello_rust)
    Finished dev [unoptimized + debuginfo] target(s) in 3.18s
  • cargo でビルドしたプログラムを実行する
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
     Running `target/debug/hello_rust`
Hello, world!
$ cargo check
    Checking hello_rust v0.1.0 (/Users/htamakos/project/learning_rust/01_hello_world/hello_rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.18s
  • cargo でリリースビルドを作成する
$ cargo build --release

# target ディレクトリ以下にreleaseディレクトリが作成され、そこに実行バイナリ等が作成されます。
$ ls -lah target/release
total 552
drwxr-xr-x@ 11 htamakos  staff   352B  2 15 00:59 .
drwxr-xr-x   5 htamakos  staff   160B  2 15 00:59 ..
-rw-r--r--   1 htamakos  staff     0B  2 15 00:59 .cargo-lock
drwxr-xr-x   3 htamakos  staff    96B  2 15 00:59 .fingerprint
drwxr-xr-x   2 htamakos  staff    64B  2 15 00:59 build
drwxr-xr-x   4 htamakos  staff   128B  2 15 00:59 deps
drwxr-xr-x   2 htamakos  staff    64B  2 15 00:59 examples
-rwxr-xr-x   2 htamakos  staff   270K  2 15 00:59 hello_rust
-rw-r--r--   1 htamakos  staff   172B  2 15 00:59 hello_rust.d
drwxr-xr-x   2 htamakos  staff    64B  2 15 00:59 incremental
drwxr-xr-x   2 htamakos  staff    64B  2 15 00:59 native

自動整形ツール rustfmt のインストール

  • Rust のツールチェインには、自動フォーマットツールである rustfmt があります。これは以下のコマンドでインストールできます。
$ rustup component add rustfmt
info: downloading component 'rustfmt'
  1.6 MiB /   1.6 MiB (100 %)   1.3 MiB/s ETA:   0 s
info: installing component 'rustfmt'
  • 使い方は以下の通り
# 以下のようにインデントがおかしい.rsファイルに対して、
$ cat main.rs                                                                                                                                    
fn main() {

println!("Hello, World");
}

# rustfmt をかけると...
$ rustfmt main.rs

# 自動的にフォーマットされます。
$ cat main.rs
fn main() {
    println!("Hello, World");
}

参考: https://doc.rust-jp.rs/the-rust-programming-language-ja/1.6/book/getting-started.htmlhttps://doc.rust-jp.rs/book/second-edition/ch01-01-installation.html