Install Rust programming language on Linux

rust Or RustyIs a modern and fast cross-platform open source system programming language with a high level of abstraction to satisfy what comes from C # and Java, designed to replace C / C ++ is. Many features, including zero-cost abstraction, movement semantics, guaranteed memory safety, threads with no data races, feature-based generics, pattern matching, type inference, minimal execution time, and efficient C binding Is provided. By popular organizations such as Canonical, Chef, Coursera, CoreOS, Dropbox, Mozilla, NPM many. Learn how to install the Rust programming language on Linux today.

Install Rust programming language on Linux

The Rust language can be installed in several different ways. The official way to recommend Rust is Rust, Rust toolchain installer. Installing rust using rustup is very easy. All you need to do is open a terminal and run the following command:

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

Or

$ curl https://sh.rustup.rs -sSf | sh

Type 1 (1) Continue installation with default values. Or type Two Customize your installation. I used the default value, so I entered 1.

Sample output:

info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to
Cargo's bin directory, located at:

  /home/sk/.cargo/bin

This can be modified with the CARGO_HOME environment variable.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /home/sk/.rustup

This can be modified with the RUSTUP_HOME environment variable.

This path will then be added to your PATH environment variable by
modifying the profile file located at:

  /home/sk/.profile

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
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>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 2019-12-19, rust version 1.40.0 (73528e339 2019-12-16)
info: downloading component 'cargo'
  4.7 MiB /   4.7 MiB (100 %) 1019.7 KiB/s in  5s ETA:  0s
info: downloading component 'clippy'
  1.8 MiB /   1.8 MiB (100 %)   1.2 MiB/s in  1s ETA:  0s
info: downloading component 'rust-docs'
 11.9 MiB /  11.9 MiB (100 %) 956.3 KiB/s in 13s ETA:  0s 
info: downloading component 'rust-std'
 18.5 MiB /  18.5 MiB (100 %)   1.4 MiB/s in 17s ETA:  0s 
info: downloading component 'rustc'
 57.8 MiB /  57.8 MiB (100 %)   1.3 MiB/s in 39s ETA:  0s
info: downloading component 'rustfmt'
  3.0 MiB /   3.0 MiB (100 %)   1.4 MiB/s in  2s ETA:  0s
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 11.9 MiB /  11.9 MiB (100 %)   2.8 MiB/s in  4s ETA:  0s
info: installing component 'rust-std'
 18.5 MiB /  18.5 MiB (100 %)   8.9 MiB/s in  6s ETA:  0s
info: installing component 'rustc'
 57.8 MiB /  57.8 MiB (100 %)   6.9 MiB/s in 13s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable'

  stable installed - rustc 1.40.0 (73528e339 2019-12-16)


Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done
automatically.

To configure your current shell run source $HOME/.cargo/env

As you can see from the output above, the rustup installer script downloads and installs the official compiler for the Rust programming language and its package manager. cargoAnd it will be added cargo, Rusty, rust Other commands to Cargo’s bin directory at ~ / .Cargo / bin. Next, add the Cargo bin directory to your PATH environment variable. profile Files in ~ / .Profile.

Finally, configure the current shell by running the following command:

$ source $HOME/.cargo/env

To determine which version you have installed:

$ rustc --version
rustc 1.40.0 (73528e339 2019-12-16)

Alright! Installed the latest Rust version. Let’s create a sample rust program and see if it works.

Testing the Rust programming language

Create a main project directory and place all your Rust code. This directory serves as the parent directory for all Rust programs.

For example, create a main project directory named: “My_rust_projects” In my $ HOME directory.

$ mkdir ~/my_rust_projects

Change to that directory.

$ cd ~/my_rust_projects

Next, create the binary ‘Hello World’ Package using Cargo as shown below.

$ cargo new hello_world

The above command creates a new directory named “hello_world” that contains all the necessary files.

CD to that directory:

$ cd hello_world

Finally, run the hello_world program using the command.

$ cargo run

Sample output:

Compiling hello_world v0.1.0 (/home/sk/my_rust_projects/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.44s
     Running `target/debug/hello_world`
Hello, world!

Running the Hello World program in Rust

This is the recommended use of Cargo.

Alternatively, you can create the project directory manually, write the code in a text file, and finally compile and run as follows:

Create a project directory.

$ mkdir hello_world

CD to that directory:

$ cd hello_world

Create your first Rust program using text or a GUI editor. Rust files always end with an extension .rs.

For example, to create a sample rust program ostechnix.rsDo:

$ vi ostechnix.rs

Copy and paste the following code.

fn main() {
    println!("Hello, Welcome To OSTechNIx blog!");
}

Save the file and exit (: wq).

Next, compile the rust code by running the following command:

$ rustc ostechnix.rs

The above command creates an executable rust program ostechnix Current directory.

$ ls
ostechnix ostechnix.rs

Finally, run the Rust program with a command.

$ ./ostechnix 
Hello, Welcome To OSTechNIx blog!

It works !!

Enable tab completion

Rustup supports common shell tab completion such as: Bash, fish, Zsh And Power shell.

To Enable Tab Completion BashDo:

$ mkdir -p ~/.local/share/bash-completion/completions
$ rustup completions bash >> ~/.local/share/bash-completion/completions/rustup

On fish:

$ mkdir -p ~/.config/fish/completions
$ rustup completions fish > ~/.config/fish/completions/rustup.fish

On Zsh:

$ mkdir ~/.zfunc

Next, add the following line to the “.zshrc” file just before “compinit”:

fpath+=~/.zfunc

Now you can install the completion script using the following command:

$ rustup completions zsh > ~/.zfunc/_rustup

After you enable tab completion, you must log out and log back in to the shell session for the changes to take effect.

Update Rust

To update rust when a new version is released:

$ rustup update
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2018-03-29, rust version 1.25.0 (84203cac6 2018-03-25)
info: downloading component 'rustc'
info: downloading component 'rust-std'
info: downloading component 'cargo'
info: downloading component 'rust-docs'
info: removing component 'rustc'
info: removing component 'rust-std'
info: removing component 'cargo'
info: removing component 'rust-docs'
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'
info: checking for self-updates

stable-x86_64-unknown-linux-gnu updated - rustc 1.25.0 (84203cac6 2018-03-25)

This command also checks for updates rust The latest version will be installed automatically.

To manually check for updates and install the latest version rust Without updating the installed toolchain, enter:

$ rustup self update

trouble shooting

Sometimes you “Error: Linker` cc` not found “ Message when compiling a program with Cargo. To fix this, install a C compiler such as GCC, as described in the following link:

  • How to fix Rust error “ linker ‘cc’ not found ” on Linux

Uninstall Rust

You can uninstall Rust Language at any time using the following command:

$ rustup self uninstall

This command removes rust from the system and all the above changes are undone.

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

Finally, delete the parent directory of the rust project.

$ rm -fr ~/my_rust_projects

And that’s all for now. You now know how to install and update Rust using rustup, create and run a sample rust program, and finally remove rust from your system. I hope this helps.

resource:

.

Sidebar