Rust est un langage de programmation open-source et aujourd'hui très populaire développé par Graydon Hoare en 2006. Il est extrêmement rapide, empêche les erreurs de segmentation et garantit la sécurité des threads et de la mémoire. Il prend en charge les abstractions à coût nul, les threads sans courses de données, la sémantique de déplacement, les liaisons C efficaces, le temps d'exécution minimal et la correspondance de modèles. Il est très similaire à C++ et peut fonctionner sur plusieurs plates-formes.
Dans ce tutoriel, je vais vous montrer comment installer le langage de programmation Rust sur Ubuntu 20.04.
Prérequis
- Un serveur exécutant Ubuntu 20.04.
- Un mot de passe root est configuré sur le serveur.
Installer Rust
Pour installer Rust, vous devrez installer Curl et d'autres packages sur votre système.
apt-get install curl build-essential make gcc -y
Après avoir installé le package Curl, téléchargez et exécutez le script d'installation de Rust comme indiqué ci-dessous :
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Il vous sera demandé de choisir les options d'installation comme indiqué ci-dessous :
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
Tapez 1 et appuyez sur Entrée pour continuer. Vous devriez obtenir le résultat suivant :
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
Après l'installation, vous devrez activer l'environnement Rust pour votre shell actuel. Vous pouvez l'activer à l'aide de la commande suivante :
source ~/.profile
source ~/.cargo/env
Ensuite, vérifiez la version de Rust à l'aide de la commande suivante :
rustc --version
Vous devriez obtenir le résultat suivant :
rustc 1.53.0 (53cb7b09b 2021-06-17)
Créer un exemple d'application avec Rust
Ensuite, vous devrez créer un exemple d'application pour tester Rust.
Commencez par créer un répertoire pour votre application :
mkdir app
Ensuite, changez le répertoire de l'application et créez un exemple d'application avec la commande suivante :
cd app
nano app.rs
Ajoutez le code suivant :
fn main() { println!("Hello World, this is my first app"); }
Enregistrez et fermez le fichier puis compilez le programme avec la commande suivante :
rustc app.rs
Cela créera un exécutable appelé app dans le répertoire courant.
Ensuite, lancez le programme avec la commande suivante :
./app
Vous devriez voir le résultat suivant :
Hello World, this is my first app
Pour mettre à jour le package Rust, exécutez la commande suivante :
rustup update
Vous devriez obtenir le résultat suivant :
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)
Si vous souhaitez supprimer Rust de votre système, exécutez la commande suivante :
rustup self uninstall
Vous devriez obtenir le résultat suivant :
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
Conclusion
Toutes nos félicitations! vous avez installé avec succès Rust sur le serveur Ubuntu 20.04. Vous pouvez maintenant écrire du code extrêmement rapide avec une très faible empreinte mémoire en utilisant Rust.