Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save okunokentaro/48ba4fc1c3c39c85b546aeaef2ea3ef4 to your computer and use it in GitHub Desktop.
Save okunokentaro/48ba4fc1c3c39c85b546aeaef2ea3ef4 to your computer and use it in GitHub Desktop.
RustのHello worldを書くので学習メモ
2016/12/23 にQiitaに投稿した記事のアーカイブです
---
@armorik83です。[Rust Advent Calendar 2016](http://qiita.com/advent-calendar/2016/rust-lang)も終盤ですが、ここへきてHello worldをやろうと思います。この記事を書き始めた段階でRust経験は0分なので、環境構築を進めながらリアルタイムに記事を書いていきます。
# 環境構築
Rustはコンパイルが必要な言語なので、まずはコンパイラ周りから準備します。『[multirustが非推奨になったようなのでrustupに移行する](http://keens.github.io/blog/2016/06/12/multirustgahisuishouninattayounanoderustupniikousuru/)』という記事があったので、[`rustup`](https://www.rustup.rs/)を入れます。
```
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
```
ここは1を選択します。これで`downloading component 'rustc'`が表示され、コンパイラのインストールが始まります。完了すると、なんかパスが通っていないので設定します。
```text:.zshrc
export PATH="$HOME/.cargo/bin:$PATH"
```
これでパスが通って`rustc`と`cargo`が触れるようになりました。[`cargo`](https://crates.io/)はパッケージマネージャだそうです。
```
$ rustc --version
rustc 1.13.0 (2c6933acc 2016-11-07)
$ cargo --version
cargo 0.13.0-nightly (eca9e15 2016-11-01)
```
# Hello world
ソースを書いて動かしてみましょう。エディタはひとまずAtomでいきます。Atomには[`language-rust`](https://atom.io/packages/language-rust)プラグインを入れました。
```rust:hello.rs
fn main() {
println!("Hello World!");
}
```
とりあえず、[公式](https://doc.rust-lang.org/book/getting-started.html#writing-and-running-a-rust-program)からこのソースを持ってきて実行してみます。
```
$ rustc hello.rs
```
ここでHello World!が出ると思ったら大間違い。コンパイルしただけなので、実行はされていません。バイナリが生成されているので、そちらを叩きます。
```
$ ./hello
Hello World!
```
できました!
## ダブルクオートとシングルクオート
```rust:hello.rs
fn main() {
println!("Hello World!");
}
```
Hello World!はダブルクオートで囲まれていますが、これはシングルクオートにするとどうなるのかやってみました。
```
$ rustc hello.rs
error: character literal may only contain one codepoint: ')
--> hello.rs:2:27
|
2 | println!('Hello World!');
|
```
怒られました。これはRustではシングルクオートは`Character literals`、ダブルクオートは`String literals`と[明確に区別されているからのようです](https://doc.rust-lang.org/reference.html#characters-and-strings)。
## セミコロン
```rust:hello.rs
fn main() {
println!("Hello World!")
}
```
次に行末のセミコロンを消してみました。するとちゃんとコンパイルされ、動きます。この点について調べてみると、公式に言及がありました。
> The line ends with a semicolon (;). Rust is an expression-oriented language, which means that most things are expressions, rather than statements. The ; indicates that this expression is over, and the next one is ready to begin. Most lines of Rust code end with a ;.
Rustは式指向の言語なので式の終わりには`;`を付けるということだそうです。慣れないうちは公式に従って付けておくほうがよさそうです。
## いくつか試す
せっかくなので他の例を試します。
```rust:hello.rs
fn print_hello_world() {
println!("Hello World!");
}
fn main() {
print_hello_world();
}
```
関数呼び出し。これは他の言語となんら変わりないです。命名規則はどうやらスネークケース (`snake_case`) なので従っておきましょう。
次にクラスを作ろう…と思ったらRustには`class`が無い?`struct`と`impl`で表現するっぽいです。
```typescript.ts
class Person {
constructor(public name: string, public age: number) {}
sayName() {
console.log(`I'm ${this.name}, ${this.age} years old.`)
}
}
const person = new Person('armorik', 83)
person.sayName()
```
このTypeScriptの`class`をRustでそれっぽく書いてみます。
```rust:hello.rs
struct Person {
name: String,
age: i32,
}
impl Person {
fn say_name(&self) {
println!("I'm {}, {} years old.", self.name, self.age);
}
}
fn main() {
let person = Person {name: "armorik".to_string(), age: 83};
person.say_name();
}
```
いくつか慣れが必要ですが。この辺りはまだなんとかなりそう。
# 最後に
1時間もあれば環境構築していろいろ試せたので、興味ある人は年末年始にササッとやってみるといいと思います。
- [プログラミング言語Rust @ GrandFrontend Osaka 2016](https://speakerdeck.com/likr/puroguraminguyan-yu-rust-at-grandfrontend-osaka-2016)
関連資料として、なぜかフロントエンドのカンファレンスでRustの話をし続けていた[こわいぱんだ](http://qiita.com/_likr)のスライドを置いておきます。それでは。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment