So I have started to learn Rust programming language and will be sharing a series of blogs with you so that we can together have insights on it, so let's dig into this immediately.
I am learning this with the help of Piyush Garg's Youtube Channel as he is learning rust too, so please check him out, he has many other great contents too!!
Introduction
Rust is a programming language of system, designed by a brilliant team that lets us write high-level software and applications. It has been built on top of safety and concurrency. Because of its low-level nature, it can give you command over systems like CPU and other units.
Setting Up
I'll be only sharing setting up Rust in Linux as I have Linux OS you can have it set up in Windows by clicking this.
Install rustup*(It's a Rust Command Line Tool to manage Rust versions) in Linux with the following command -*
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
if you see a message Rust is installed on your system!! then you are good to go.
To check which version is installed, just type the below commands -
$ rustc --version
Writing the first program
So as per old tradition, any programming language you learn should be started with writing a sweet and simple Hello, World!! program, as if you don't write it you are a sinner😈.
fn main() {
println!("Hello, World!!");
}
to run this program simply click on the run button or if you wanna be a pro, type these commands -
$ rustc main.rs
this command will compile your Rust program making sure there is no error in it, which may bring you to another great point which is "Rust is a compiled time programming language" which means errors or dangers would be checked only when you compile the program, unlike Python and JS or any another programming language which is dynamically typed.
To run the program, simply type this command -
./main
This would generate a "Hello, World!!" message.
Now let's understand the syntax one by one
fn main(){} - This is a very special function in Rust. For every program that would be made, there had to be a main function and this was the block that would be running first after execution. () These paratheses will contain parameters, here we don't have any so it's empty.
println!("Hello, world!"); - This line is the main part of the program—it displays text on the screen. There are four things to notice:
Rust uses four spaces for indentation, not tabs.
println!
is a Rust macro, not a regular function. If it were a function, it would be written asprintln
without the!
.The string
"Hello, world!"
is passed toprintln!
, which prints it on the screen.The line ends with a semicolon (
;
), showing that this statement is finished, which is common in Rust.
Writing program with Cargo!
Cargo is Rust's build system and package manager, designed to simplify project management by automating many tasks. When you create a new project with the command cargo new project_name
, Cargo sets up the project with a Cargo.toml
(Tom's Obvious Minimal Language) file and a src
folder.
In Rust, packages of code are called crates. By default, Cargo builds a debug version of your project, not a production-ready one. The Cargo.lock
file tracks the exact versions of your dependencies, similar to package-lock.json
in Node.js.
You can use cargo run
to compile and run your project automatically, without needing to navigate into the src
folder manually. Additionally, cargo check
allows you to quickly verify that your code compiles without actually building the executable, which saves time during development.
Cargo is incredibly efficient and smart, streamlining the development process in Rust.
Creating packages with cargo
To create a new package with cargo just hit this command -
$ cargo new hello_cargo
$ cd hello_cargo
To build the program write this command -
$ cargo build
and this would build a debug-level program after compiling it and you can run it via -
cargo run
Remember this would run your program and after this, if you make any changes and run this command, you will see that cargo builds it automatically for you. So this can come in handy when you make big programs.
Also as per docs of Rust, you can use cargo build --release if you want your project to be released in production.
Conclusion
Congratulations, you're a Rust programmer now (well I wouldn't say that because there is much to learn). We have covered just the introduction and setting up of our Rust Anatomy, as I proceed further learning it I will make more Blogs.
Till then if you too want to learn the same way just check this Youtube Playlist and RustBook Guide -