Comment installer le langage de programmation Rust sur Ubuntu 20.04
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, évite les segfaults et garantit la sécurité des threads et de la mémoire. Il prend en charge les abstractions à coût zéro, les threads sans courses de données, la sémantique des déplacements, les liaisons C efficaces, le temps d’exécution minimal et la correspondance des motifs. Il est très similaire à C++ et peut fonctionner sur plusieurs plateformes.
Dans ce tutoriel, je vais te montrer comment installer le langage de programmation Rust sur Ubuntu 20.04.
Conditions préalables
- Un serveur exécutant Ubuntu 20.04.
- Un mot de passe root est configuré sur le serveur.
Installer Rust
Pour installer Rust, tu dois installer le paquet Curl et d’autres paquets sur ton système.
apt-get install curl build-essential make gcc -y
Après avoir installé le paquet Curl, télécharge et exécute le script d’installation de Rust comme indiqué ci-dessous :
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Il te 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
Tape 1 et appuie sur Entrée pour continuer. Tu devrais obtenir la sortie suivante :
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, tu devras activer l’environnement Rust pour ton shell actuel. Tu peux l’activer à l’aide de la commande suivante :
source ~/.profile
source ~/.cargo/env
Ensuite, vérifie la version de Rust à l’aide de la commande suivante :
rustc --version
Tu devrais obtenir le résultat suivant :
rustc 1.53.0 (53cb7b09b 2021-06-17)
Créer une application type avec Rust
Ensuite, tu devras créer une application type pour tester Rust.
Tout d’abord, crée un répertoire pour ton application :
mkdir app
Ensuite, change le répertoire en app et crée une application d’exemple avec la commande suivante :
cd app
nano app.rs
Ajoute le code suivant :
fn main() { println!("Hello World, this is my first app"); }
Sauvegarde et ferme le fichier puis compile le programme avec la commande suivante :
rustc app.rs
Cela créera un exécutable appelé app dans le répertoire actuel.
Ensuite, exécute le programme avec la commande suivante :
./app
Tu devrais voir le résultat suivant :
Hello World, this is my first app
Pour mettre à jour le paquet Rust, exécute la commande suivante :
rustup update
Tu devrais 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 tu veux supprimer le Rust de ton système, exécute la commande suivante :
rustup self uninstall
Tu devrais 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
Félicitations ! Tu as réussi à installer Rust sur le serveur Ubuntu 20.04. Tu peux maintenant écrire du code extrêmement rapide avec une empreinte mémoire très faible en utilisant Rust.