Introduction
Rust is a robust, reliable, productive language(check rust language benchmarks). The language has all the tools to write program efficiently and It also has a well written userfriendly documentation. In this tutorial, we will write our first Rust programme on a Linux machine and Introduce some syntax rules in Rust language.
Installing Rust in Linux.
We can install Rust in Linux easily. To download and install Rust you just need to copy and paste the below command and press enter key then it will download and install Rust into your machine using a tool called rustup. rustup is a command line tool for rust installer and version management.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
terminal:
bash-5.1$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
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:
/home/user/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
/home/user/.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:
/home/user/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/home/user/.profile
/home/user/.bash_profile
/home/user/.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
when executing this command it will prompt you to enter installation options with a list of three options like below.
terminal:
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 2022-09-22, rust version 1.64.0 (a55dd71d5 2022-09-19)
info: downloading component 'cargo'
6.4 MiB / 6.4 MiB (100 %) 809.6 KiB/s in 7s ETA: 0s
info: downloading component 'clippy'
2.8 MiB / 2.8 MiB (100 %) 1023.7 KiB/s in 3s ETA: 0s
info: downloading component 'rust-docs'
18.8 MiB / 18.8 MiB (100 %) 1.1 MiB/s in 17s ETA: 0s
info: downloading component 'rust-std'
27.4 MiB / 27.4 MiB (100 %) 1.2 MiB/s in 25s ETA: 0s
info: downloading component 'rustc'
54.2 MiB / 54.2 MiB (100 %) 1.2 MiB/s in 1m 1s ETA: 0s
info: downloading component 'rustfmt'
4.3 MiB / 4.3 MiB (100 %) 1.2 MiB/s in 4s ETA: 0s
info: installing component 'cargo'
6.4 MiB / 6.4 MiB (100 %) 6.4 MiB/s in 1s ETA: 0s
info: installing component 'clippy'
info: installing component 'rust-docs'
18.8 MiB / 18.8 MiB (100 %) 424.0 KiB/s in 41s ETA: 0s
info: installing component 'rust-std'
27.4 MiB / 27.4 MiB (100 %) 3.2 MiB/s in 11s ETA: 0s
6 IO-ops / 6 IO-ops (100 %) 0 IOPS in 15s ETA: Unknown
info: installing component 'rustc'
54.2 MiB / 54.2 MiB (100 %) 4.7 MiB/s in 19s 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.64.0 (a55dd71d5 2022-09-19)
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"
choose the default option for your installation, and then it will download the necessary components and show you the message Rust is installed. After that enter the following command to set the environment variable.
source "$HOME/.cargo/env"
now type rustc --version and cargo --version commands on your terminal to check whether Rust has been installed successfully. In this commands rustc stands for Rust compiler and cargo is a Rust package manager.
Writing Your First Rust Program
For writing our program we use VS Code IDE. we assume you already installed the VS code. In vs code first, go to the extensions and install the rust-analyzer extension which includes the support for Rust programming language. Then create a file called hello_world.rs in a directory and write or paste the below code.
fn main() {
println!("Hello world!");
}
Running Your Program
![]() |
Rust compilation and execution steps |
To run the program first you need to compile your program using the Rust compiler, to do that navigate to the directory which contains hello_world.rs then you need to run the below command in your terminal.
bash-5.1$ rustc hello_world.rs
After the compilation process is over you can see a new file created in the directory named hello_world This is a binary file created by the compilation process. then you can execute the program in Linux by running the below command.
bash-5.1$ ./hello_world
Hello world!
Dissecting Your First Rust Program
in our hello world rust program first, you see an outer block of the program as fn main(){}. fn is the keyword used in Rust to declare a function and the main is the name of the function followed by pair of opening and closing parentheses and then pair of curly brackets which specify the main function body.
This main function is the starting point of our program. In Rust function main holds a special place like java and C++ Because It’s the first thing to run in a Rust program.
Inside the main function body, there is a single line println!("Hello world!"); that prints the output to the terminal Hello world!. In this line println! is called a Rust macro. We’ll discuss Rust macro in another lesson. We pass a string hello world! To Rust macro as the argument.
Rust statement end by typing a (;) semicolon at the end. In Rust, we indicate the end of the statements by typing a semicolon(;)
No comments:
Post a Comment