Rustはオープンソースであり、2006年にGraydon Hoareによって開発された現在非常に人気のあるプログラミング言語です。非常に高速で、segfaultを防ぎ、スレッドとメモリの安全性を保証します。ゼロコストの抽象化、データ競合のないスレッド、移動セマンティクス、効率的なCバインディング、最小限のランタイム、およびパターンマッチングをサポートします。これはC++と非常によく似ており、複数のプラットフォームで実行できます。
このチュートリアルでは、Ubuntu20.04にRustプログラミング言語をインストールする方法を紹介します。
- Ubuntu20.04を実行しているサーバー。
- ルートパスワードはサーバーで構成されています。
Rustをインストールするには、Curlおよびその他のパッケージをシステムにインストールする必要があります。
apt-get install curl build-essential make gcc -y
Curlパッケージをインストールした後、以下に示すようにRustインストールスクリプトをダウンロードして実行します。
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 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 installation (default) 2) Customize installation 3) Cancel installation >1
1と入力し、Enterキーを押して続行します。次の出力が得られるはずです:
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 2021-06-17, rust version 1.53.0 (53cb7b09b 2021-06-17) info: downloading component 'cargo' info: downloading component 'clippy' info: downloading component 'rust-docs' info: downloading component 'rust-std' info: downloading component 'rustc' 48.4 MiB / 48.4 MiB (100 %) 26.8 MiB/s in 1s ETA: 0s info: downloading component 'rustfmt' info: installing component 'cargo' info: installing component 'clippy' info: installing component 'rust-docs' 16.1 MiB / 16.1 MiB (100 %) 1.9 MiB/s in 6s ETA: 0s info: installing component 'rust-std' 25.3 MiB / 25.3 MiB (100 %) 5.8 MiB/s in 4s ETA: 0s info: installing component 'rustc' 48.4 MiB / 48.4 MiB (100 %) 7.1 MiB/s in 7s 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.53.0 (53cb7b09b 2021-06-17) 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, run: source $HOME/.cargo/env
インストール後、現在のシェルのRust環境をアクティブ化する必要があります。次のコマンドを使用してアクティブ化できます:
source ~/.profile
source ~/.cargo/env
次に、次のコマンドを使用してRustのバージョンを確認します。
rustc --version
次の出力が得られるはずです:
rustc 1.53.0 (53cb7b09b 2021-06-17)
Rustを使用してサンプルアプリケーションを作成する
次に、Rustをテストするためのサンプルアプリケーションを作成する必要があります。
まず、アプリケーションのディレクトリを作成します。
mkdir app
次に、ディレクトリをアプリに変更し、次のコマンドを使用してサンプルアプリケーションを作成します。
cd app
nano app.rs
次のコードを追加します:
fn main() { println!("Hello World, this is my first app"); }
ファイルを保存して閉じてから、次のコマンドを使用してプログラムをコンパイルします。
rustc app.rs
これにより、現在のディレクトリにappという実行可能ファイルが作成されます。
次に、次のコマンドを使用してプログラムを実行します。
./app
次の出力が表示されます。
Hello World, this is my first app
Rustパッケージを更新するには、次のコマンドを実行します。
rustup update
次の出力が得られるはずです:
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' info: checking for self-updates stable-x86_64-unknown-linux-gnu unchanged - rustc 1.53.0 (53cb7b09b 2021-06-17)
システムからRustを削除する場合は、次のコマンドを実行します。
rustup self uninstall
次の出力が得られるはずです:
Thanks for hacking in Rust! This will uninstall all Rust toolchains and data, and remove $HOME/.cargo/bin from your PATH environment variable. Continue? (y/N) y info: removing rustup home info: removing cargo home info: removing rustup binaries info: rustup is uninstalled
おめでとう!これで、Ubuntu20.04サーバーにRustが正常にインストールされました。 Rustを使用して、非常に低いメモリフットプリントで非常に高速なコードを記述できるようになりました。