Closed4
RustでHello World
Ubuntu 22.04コンテナで。
$ docker run -it --rm ubuntu /bin/bash
インストール
$ apt update
$ apt install -y curl vim gcc
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
対話形式になる
info: downloading installer
Welcome to Rust!
This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
/root/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
/root/.cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:
/root/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/root/.profile
/root/.bashrc
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
default host triple: x86_64-unknown-linux-gnu
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
>
1
で進める
>1
info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2024-02-08, rust version 1.76.0 (07dca489a 2024-02-04)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
14.7 MiB / 14.7 MiB (100 %) 10.0 MiB/s in 1s ETA: 0s
info: downloading component 'rust-std'
23.9 MiB / 23.9 MiB (100 %) 9.9 MiB/s in 2s ETA: 0s
info: downloading component 'rustc'
62.3 MiB / 62.3 MiB (100 %) 8.0 MiB/s in 7s ETA: 0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
info: installing component 'rust-std'
23.9 MiB / 23.9 MiB (100 %) 23.4 MiB/s in 1s ETA: 0s
info: installing component 'rustc'
62.3 MiB / 62.3 MiB (100 %) 25.6 MiB/s in 2s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'
stable-x86_64-unknown-linux-gnu installed - rustc 1.76.0 (07dca489a 2024-02-04)
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).
To configure your current shell, you need to source
the corresponding env file under $HOME/.cargo.
This is usually done by running one of the following (note the leading DOT):
. "$HOME/.cargo/env" # For sh/bash/zsh/ash/dash/pdksh
source "$HOME/.cargo/env.fish" # For fish
環境変数を読み込む
$ . "$HOME/.cargo/env"
ちなみにこれ何してんのかなと思って見てみたけどパスしか設定してない
#!/bin/sh
# rustup shell setup
# affix colons on either side of $PATH to simplify matching
case ":${PATH}:" in
*:"$HOME/.cargo/bin":*)
;;
*)
# Prepending path in case a system-installed rustc needs to be overridden
export PATH="$HOME/.cargo/bin:$PATH"
;;
esac
パスの確認
$ which rustc
/root/.cargo/bin/rustc
$ ls /root/.cargo/bin/
cargo cargo-fmt clippy-driver rust-analyzer rust-gdbgui rustc rustfmt
cargo-clippy cargo-miri rls rust-gdb rust-lldb rustdoc rustup
hello worldをやってみる。
hello.rs
fn main() {
println!("Hello World!");
}
コンパイル
$ rustc hello.rs
バイナリができる
$ ls -lt hello*
-rwxr-xr-x 1 root root 3784344 Mar 13 10:24 hello
-rw-r--r-- 1 root root 44 Mar 13 10:21 hello.rs
実行
$ ./hello
Hello World!
rustupでバージョンを管理できる。
まず現在。
$ rustc --version
rustc 1.76.0 (07dca489a 2024-02-04)
現在インストールされているのはstable=1.76ということらしい。
$ rustup toolchain list
stable-x86_64-unknown-linux-gnu (default)
v1.75.0を追加する
$ rustup toolchain add 1.75
info: syncing channel updates for '1.75-x86_64-unknown-linux-gnu'
info: latest update on 2023-12-28, rust version 1.75.0 (82e1608df 2023-12-21)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
14.3 MiB / 14.3 MiB (100 %) 9.7 MiB/s in 1s ETA: 0s
info: downloading component 'rust-std'
23.6 MiB / 23.6 MiB (100 %) 9.1 MiB/s in 2s ETA: 0s
info: downloading component 'rustc'
61.4 MiB / 61.4 MiB (100 %) 8.8 MiB/s in 7s ETA: 0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
info: installing component 'rust-std'
23.6 MiB / 23.6 MiB (100 %) 23.3 MiB/s in 1s ETA: 0s
info: installing component 'rustc'
61.4 MiB / 61.4 MiB (100 %) 25.9 MiB/s in 2s ETA: 0s
info: installing component 'rustfmt'
1.75-x86_64-unknown-linux-gnu installed - rustc 1.75.0 (82e1608df 2023-12-21)
info: checking for self-update
確認。1.75が追加されているのがわかる。
$ rustup toolchain list
stable-x86_64-unknown-linux-gnu (default)
1.75-x86_64-unknown-linux-gnu
切り替える
$ rustup default 1.75-x86_64-unknown-linux-gnu
info: using existing install for '1.75-x86_64-unknown-linux-gnu'
info: default toolchain set to '1.75-x86_64-unknown-linux-gnu'
1.75-x86_64-unknown-linux-gnu unchanged - rustc 1.75.0 (82e1608df 2023-12-21)
確認してみる。
$ rustc --version
rustc 1.75.0 (82e1608df 2023-12-21)
rustupでインストールする際に入力せずにそのまま実行させる
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
とりあえずこれでDockerfile内でrustをインストールするための最低限の情報は集まったのではないかと。
このスクラップは2024/03/13にクローズされました