Why Rust?
Before starting this series and my articles related to Rust I should address why am I even trying to learn and program in Rust. Honestly the first and most important reason is I feel like doing it, that doesn’t mean I do not have a better reason. It’s just a rush of learning something new making mistakes and growing my knowledge and my skillset. Also I feel like the path that I am taking as a software developer has a pretty good spot for Rust. Actually that’s it, I cannot wait to see if Rust can make me fall in love with it or just be another language in my skillset!
Note : For now I am following the official - The Rust Programming Language book.
Introduction
Rust is a multi-purpose system programming language. It is designed for performance, thread safety and memory safety. It provides low level control like C or C++ and provide modern features like many modern programming languages.
Rust Compiler : rustc
Rust uses the rustc compiler that is written in Rust itself. It directly compiles the code to machine code. So for example if we create a file called hello.rs we can directly use the Rust compiler to compile this file and it will provide us with an executable.
rustc hello.rs
./hello
Fun Fact : While researching on this topic I got to know that Rust was very much inspired by OCaml and the very first Rust compiler was written in OCaml which then enabled to make a Rust based compiler.
First Program
We create a Rust file giving it a file extension of .rs. In Rust we go by the basic rule of creating a main function that becomes the entry point for our application. We start by creating a main function that will be ran when we run the executable that is created after compiling the file.
For this example we will go the traditional route and create a “Hello, World!” program.
// hello.rs file.
fn main() {
println!("Hello, World!")
}
The above is a simple Rust code to print “Hello, World!” in the terminal. Here we have a simple main function which we can define using the keyword fn . Then inside the main function we have a println! . It is a macro according to the official guide book that is still to be explored by me. Basically if we have a ! after the name it is a macro and if it’s not there it’s a function.
Now we will see how we can manually compile and run the script like in the above section.
$ rustc hello.rs
$ ./hello
$ Hello, World!
CARGO : The level up!
Now since we have seen how can we manually compile a Rust program let’s do one thing, let’s not just use it anytime and start using Cargo! Okay so you see like in every other language we have some sort of a package manager/build system, that’s basically Cargo for Rust. It is a package manager and a build system that is used to download the libraries/dependencies that are needed in our project, build those dependencies, build the project and run it.
To get started with a project using cargo we will use the cargo new <project_name> command. It will create a basic project structure and some configuration files. Let’s first explore the file structure then look at how to actually use cargo properly. When we run the cargo new command we are presented with this type of file structure :
- <project_name>/
- Cargo.toml
- .gitignore
- src/
- main.rs
Okay so here .gitignore contains only one thing that is the /target directory since that is the build directory that we will explore later. .gitignore is created since Cargo automatically initialises git for us. Next we have the Cargo.toml file that we can relate to package.json in JavaScript. It contains the configurations for the project including the package versions and the dependency list. And at the last we have a src/ folder that contains the main.rs file that will start with when Cargo is used to build the project.
That is it for this article! We will explore CARGO deeply in the next article. Hope you all have a wonderful day ahead of you.
-Aditya
Top comments (2)
Great and a helpful article.
Thanks!