2015年に The Rust Programming Language のバージョン 1.0 がリリースされたので、試してみる。
Rustは並列かつマルチパラダイムのプログラミング言語である。Mozillaによって開発されている。純関数型プログラミング、並列アクターモデル、手続き型プログラミング、オブジェクト指向プログラミングをサポートする実用的な言語を目指している。
Rust (プログラミング言語) - Wikipedia
Homebrew で Mac にインストール
今回の環境は Mac OS X El Capitan。
Rust を探したら Homebrew にあった。
$ brew search rust
multirust rust uncrustify
homebrew/completions/rustc-completion Caskroom/cask/torustrooper
homebrew/emacs/rust-mode Caskroom/cask/uncrustifyx
Caskroom/cask/rust Caskroom/cask/virustotaluploader
Caskroom/cask/thrustshell
Homebrew で Rust をインストール。
$ brew install rust
==> Installing rust
==> Downloading https://homebrew.bintray.com/bottles/rust-1.7.0.el_capitan.bottl
######################################################################## 100.0%
==> Pouring rust-1.7.0.el_capitan.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
zsh completion has been installed to:
/usr/local/share/zsh/site-functions
==> Summary
🍺 /usr/local/Cellar/rust/1.7.0: 10,323 files, 228.2M
Rust は rust っていうコマンド名かと思ったらちがった。
$ rust --version
-bash: rust: command not found
rustc コマンドを使うらしい。
$ rustc --version
rustc 1.7.0
hello world してみる
Getting Started を参考にして Hello World を書いてみる。
$ cat ./hello.rs
fn main() {
println!("Hello, world!");
}
rustc でコンパイルして実行ファイルをつくる。
$ rustc hello.rs
$ ls
hello hello.rs
実行結果。
$ ./hello
Hello, world!
match を使ってみる
制御構造には if, else, do, while, for などがある。以上のようにC言語風であるが、C言語のキーワードが(当然だが)全てあるわけではなく、一方で多方向分岐の match 文など、あまり馴染みがないキーワードもある。
Rust (プログラミング言語) - Wikipedia
タプルをパターンマッチしてみる。
$ cat ./match.rs
fn main() {
for i in 0..2 {
for j in 0..2 {
let t = (i, j);
print(t);
}
}
}
fn print(t: (i64, i64)) {
match t {
(0, 0) => println!("nothing"),
(1, 0) => println!("one and zero"),
(x, y) => println!("{} and {}", x, y)
}
}
$ rustc ./match.rs
$ ./match
nothing
0 and 1
one and zero
1 and 1
なかなか使い勝手が良さそうな雰囲気のプログラミング言語だ( ´∀`)
ref. The Rust Programming Language
tags: rust
Posted by NI-Lab. (@nilab)