Reading time: 3 minutes
Hey everyone! Recently I started learning the Rust programming language for a personal side project of mine. But there aren't many resources available apart from the official documentation. So I decided I might as well share the stuff I have learned in Rust.
So without further ado, let's learn Rust!
How is Rust different?
Rust is a low-level programming language like C. It's a compiled language like C. But unlike C, Rust is memory-safe (No segfaults nightmares). And unlike many languages, Rust doesn't use a garbage collector. This "best of both worlds"-feature makes Rust insanely fast and efficient.
The main factor that makes it different is its memory management. I will cover this in a later tutorial. But to give you a brief idea, Rust language works on ownership and borrowing concepts of memory. In other words, when data are being passed on to different variables, the data don't get copied to all these variables. Instead, the one that currently has the data will have ownership of it while the other variables "lose" ownership of it.
Now let's get to the interesting part.
Writing your first program in Rust
Before writing your first program in Rust, let's install Rust!
Installing Rust
The installation differs for each platform. But I will be installing Rust for Windows.
Go to this link to install Rust for Windows.
Click on the button that downloads the rustup installer. Mine is Windows 10 64-bit. So I will download a 64-bit installer.
Go to the folder where you downloaded the installer and open it.
You will get the below screen:
- Type "1" and then press Enter. Wait until everything is installed. Also, Rust requires Visual Studio C++ Build Tools, which will be prompted by the installer for you to install these tools.
Once everything is done, you will get the below image which implies Rust is finally installed. Press Enter.
Setup Rust in VS Code
After the installation, create a new folder and open it in VS Code.
- Go to Extensions. Search for "rust-analyzer" and install it.
- Also, search for "CodeLLDB" and install it.
Writing Rust code
Every program in Rust ends with the file extension .rs
.
Create a file and name it
main.rs
.Like C, C++, and Java, Rust runs your main program inside a main function.
Now, write your program.
fn main() {
println!("Hello World!");
}
- Now, run the below command on your terminal to compile the Rust code.
rustc main.rs
After running this command, you will see some files including an executable file.
Now finally, run the below command to see the output.
shell
./main
This will run the main.exe
file and print "Hello World" in your terminal.
Great job! You have written in your first program in Rust!
Plus, you have also learned how to print texts in the terminal using println!()
as well as initializing a function using the fn
keyword.
In the next tutorial, we will be using Rust's in-built package manager called Cargo to make simple Rust programs and build Rust projects.
Have a great day!
GitHub Repo: https://github.com/khairalanam/rust-tutorial
If you like whatever I write here, follow me on Devto and check out my socials:
LinkedIn: https://www.linkedin.com/in/khair-alanam-b27b69221/
Twitter: https://www.twitter.com/khair_alanam
GitHub: https://github.com/khairalanam
Top comments (0)