Rust入門するので "Hello World!!" します。 内容は https://doc.rust-lang.org/book/getting-started.html を途中までやった時のメモです。
インストール
curl https://sh.rustup.rs -sSf | shinfo: 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/sximada/.cargo/bin
This path will then be added to your PATH environment variable by modifying the
profile file located at:
/Users/sximada/.profile
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
info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: downloading component 'rustc'
32.3 MiB / 32.3 MiB (100 %) 4.1 MiB/s ETA: 0 s
info: downloading component 'rust-std'
43.1 MiB / 43.1 MiB (100 %) 5.6 MiB/s ETA: 0 s
info: downloading component 'cargo'
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: default toolchain set to 'stable'
stable installed - rustc 1.16.0 (30cf806ef 2017-03-10)
Rust is installed now. Great!
To get started you need Cargo's bin directory 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/env` を読み込みます(これはbashrcにでも記載しておくと良いでしょう)。
source $HOME/.cargo/envversionを確認します。
rustc --versionrustc 1.16.0 (30cf806ef 2017-03-10)
Hello worldをprintするだけの処理を書く
fn main() {
println!("Hello World");
}ビルド
シンプルなコードなのでrustcを直接用いてビルドすることにした。 rustcコマンドの第一引数にソースコードへのパスを指定する。
rustc src/main.rsmainという実行ファイルが作成される。
実行する
作成されたmainという実行ファイルを実行する。
$ ./main Hello world! $
Cargoを使ってビルドする
Cargoはビルドシステム兼パッケージマネージャです。設定ファイルCargo.tomlはTOML形式で次のように記載します。
[package]
name = "hello_world"
version = "2017.4.1"
authors = ["sximada <[email protected]>"]ビルドします。
cargo buildこのようにファイルが生成されます。
target
└── debug
├── build
├── deps
│ ├── hello_world-586190e903bcb503
│ └── hello_world-586190e903bcb503.dSYM
│ └── Contents
│ ├── Info.p
│ └── Resources
│ └── DWARF
│ └── hello_world-586190e903bcb503
├── examples
├── hello_worl
├── hello_worl
├── incremental
└── native
target/debug/hello_world が実行ファイルです。実行します。
./target/debug/hello_worldHello World
参考
https://doc.rust-jp.rs/book-ja/ch01-02-hello-world.html