There are different ways to install Rust toolchain in your system. You can compile it from scratch or use your OS package manager. But the easiest way to install Rust toolchain is by using rustup, which is an installer for Rust. You can visit rustup.rs and follow the instructions to install it.
Installing rustup on the Linux OS or macOS
If you are using Debian 10 Linux distribution, the following instructions are applicable. However, if you are already using a different Linux distribution, we will assume that you are familiar with the Linux operating system and can adjust these instructions to suit your Linux distribution.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
It will then show a prompt, which you can customize; for now, we're just going to use the default setup:
...
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>
Type 1
to use the default installation.
After that, reload your terminal or type this in the current terminal:
source $HOME/.cargo/env
You can confirm whether the installation was successful or not by typing rustup
in the Terminal and you should see the usage instruction for rustup.
Now, let's install the stable Rust toolchain. Type the following in the terminal:
rustup toolchain install stable
After the toolchain has been installed into your OS, let's confirm whether we can run the Rust compiler. Type rustc
in the terminal and you should see the instructions on how to use it.
Updating the toolchain, rustup, and components
The Rust toolchain has a regular release schedule of around every three months (six weeks plus six weeks), but sometimes there's an emergency release for a major bug fix or a fix for a security problem. As a result, you sometimes need to update your toolchain.
Updating is very easy. This command will also update the components installed in the toolchain:
rustup update
Besides the toolchain, rustup
itself might also be updated. You can update it by typing the following:
rustup self update
Now that we have the Rust compiler toolchain installed in our system, let's write our first Rust program!
Writing Hello World!
We are going to write a very basic program, Hello World!. After we successfully compile that, we are going to write a more complex program to see the basic capabilities of the Rust language. Let's do it by following these instructions:
Let's create a new folder, for example,
try-rust
.Create a new file inside the folder and give it the name
main.rs
.Let's write our first code in Rust:
fn main() { println!("Hello World!"); }
After that, save your file, and in the same folder, open your terminal, and compile the code using the
rustc
command:rustc main.rs
You can see there's a file inside the folder called
main
; run that file from your terminal:./main
Congratulations! You just wrote your first
Hello World
program in the Rust language.
Closing
Rust is a promising and powerful programming language that offers excellent memory safety and performance. With the resources and tips outlined in this guide, you can get started with Rust programming and take advantage of its unique features. Whether you are a seasoned developer or a beginner, Rust provides an exciting opportunity to create efficient, reliable, and safe software. Keep exploring, learning, and experimenting with Rust to unlock its full potential!
Keep an eye out for future posts where I'll delve into even more interesting topics. In the meantime, I hope this information has been helpful and I look forward to your continued engagement. Thanks again!