Rustによる"Hello World"のプログラム開発を行う方法。
パッケージ作成
Rustの開発では、最初に以下のコマンドを実行して、プログラム開発に必要なディレクトリやファイル等を作成します。
cargo new [パッケージ名]
"Hello World"のパッケージ名を "helloworld"とすると、以下を実行します。
cargo new helloworldこれを実行すると以下のようになります。
$ cargo new helloworld
Created binary (application) `helloworld` package
正常に完了すると、現在のディレクトリに "helloworld" ディレクトリが作成され、Rust開発用ファイルが作成されます。
$ tree -a
.
└── helloworld
├── Cargo.toml
└── src
└── main.rs
2 directories, 2 files
パッケージのビルド
ビルドは、パッケージのトップディレクトリで以下を実行します。
cargo build
例
$ cargo build
Compiling helloworld v0.1.0 (/home/vagrant/rust/helloworld)
Finished dev [unoptimized + debuginfo] target(s) in 0.74s
ビルドが成功すると、以下のディレクトリ、ファイル構成になります。
$ tree -a
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
├── CACHEDIR.TAG
├── debug
│ ├── build
│ ├── .cargo-lock
│ ├── deps
│ │ ├── helloworld-129fc09c03e69dbb
│ │ └── helloworld-129fc09c03e69dbb.d
│ ├── examples
│ ├── .fingerprint
│ │ └── helloworld-129fc09c03e69dbb
│ │ ├── bin-helloworld
│ │ ├── bin-helloworld.json
│ │ ├── dep-bin-helloworld
│ │ └── invoked.timestamp
│ ├── helloworld
│ ├── helloworld.d
│ └── incremental
└── .rustc_info.json
9 directories, 14 files
実行
ビルドが成功したら、以下のコマンドでビルドしたパッケージを実行します。
cargo run
エラーがなければ以下のようになります。
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
Running `target/debug/helloworld`
Hello, world!