Ricerca nel sito web

Come installare il linguaggio di programmazione Rust su Ubuntu 20.04


Questo tutorial esiste per queste versioni del sistema operativo

  • Ubuntu 22.04 (Jammy Jellyfish)
  • Ubuntu 20.04 (Focal Fossa)

Su questa pagina

  1. Prerequisiti
  2. Installa Rust
  3. Crea un'applicazione di esempio con Rust
  4. Conclusione

Rust è un linguaggio di programmazione open-source e oggi molto popolare sviluppato da Graydon Hoare nel 2006. È estremamente veloce, previene i segfault e garantisce la sicurezza di thread e memoria. Supporta astrazioni a costo zero, thread senza corse di dati, semantica di spostamento, associazioni C efficienti, tempo di esecuzione minimo e corrispondenza dei modelli. È molto simile a C++ e può essere eseguito su diverse piattaforme.

In questo tutorial, ti mostrerò come installare il linguaggio di programmazione Rust su Ubuntu 20.04.

Prerequisiti

  • Un server che esegue Ubuntu 20.04.
  • Sul server è configurata una password di root.

Installa ruggine

Per installare Rust, dovrai installare Curl e altri pacchetti sul tuo sistema.

apt-get install curl build-essential make gcc -y

Dopo aver installato il pacchetto Curl, scarica ed esegui lo script di installazione di Rust come mostrato di seguito:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Ti verrà chiesto di scegliere le opzioni di installazione come mostrato di seguito:

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

Digita 1 e premi Invio per continuare. Dovresti ottenere il seguente output:

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

Dopo l'installazione, sarà necessario attivare l'ambiente Rust per la shell corrente. Puoi attivarlo usando il seguente comando:

source ~/.profile
source ~/.cargo/env

Successivamente, verifica la versione di Rust usando il seguente comando:

rustc --version

Dovresti ottenere il seguente output:

rustc 1.53.0 (53cb7b09b 2021-06-17)

Crea un'applicazione di esempio con Rust

Successivamente, dovrai creare un'applicazione di esempio per testare Rust.

Innanzitutto, crea una directory per la tua applicazione:

mkdir app

Successivamente, modifica la directory nell'app e crea un'applicazione di esempio con il seguente comando:

cd app
nano app.rs

Aggiungi il seguente codice:

fn main() {
    println!("Hello World, this is my first app");
}

Salvare e chiudere il file quindi compilare il programma con il seguente comando:

rustc app.rs

Questo creerà un eseguibile chiamato app nella directory corrente.

Quindi, esegui il programma con il seguente comando:

./app

Dovresti vedere il seguente output:

Hello World, this is my first app

Per aggiornare il pacchetto Rust, esegui il seguente comando:

rustup update

Dovresti ottenere il seguente output:

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)

Se vuoi rimuovere Rust dal tuo sistema, esegui il seguente comando:

rustup self uninstall

Dovresti ottenere il seguente output:

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

Conclusione

Congratulazioni! hai installato correttamente Rust sul server Ubuntu 20.04. Ora puoi scrivere codice estremamente veloce con un footprint di memoria molto basso utilizzando Rust.