🦀
RustでHello World
はじめに
RustのインストールからHello Worldを出すところまでをやってみようと思います。
環境
- macOS Ventura
インストールしてみる
公式に書いてあるインストールコマンドを実行します。
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
コマンド実行後、途中で下記を聞かれるので1を選択します。
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
インストールが終わったら環境変数にパスを通します。
$ source "$HOME/.cargo/env"
インストールが完了すると、cargo, rustc, rustupの3つのツールが使えるようになるので、
試しにバージョンが表示してみます。
※2023/03/22に実行したときのバージョンです。
$ cargo --version
cargo 1.68.0 (115f34552 2023-02-26)
$ rustc --version
rustc 1.68.0 (2c8cc3432 2023-03-06)
$ rustup --version
rustup 1.25.2 (17db695f1 2023-02-01)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.68.0 (2c8cc3432 2023-03-06)`
Hello Worldを出してみる
任意の場所にhello_world.rsファイルを作成する。
作成したファイルに次の内容を書きます。
fn main () {
println!("Hello World");
}
書き込んだらコンパイルしてみます。
$ rustc hello_world.rs
コンパイルが成功すると、hello_world.rsと同じ場所にhello_worldが作成されます。
$ ls
$ hello_world hello_world.rs
最後に実行します。
$ ./hello_world
$ Hello World ← 実行結果
Discussion