Why Rust & What Is Rust

Reading Time: 2 minutes

Why Rust? 

In system programming languages we basically face two complex problems which are difficult to crack i.e. Secure and Multithreaded code.
For this Rust has added its own goals of memory safety and data-race-free concurrency.

What Is Rust? 

Rust is a System programing language sponsored by Mozilla which describes it as a “safe,  concurrent and practical language,” supporting functional and imperative-procedural paradigms.
It basically focused on three goals i.e. safety, speed, and concurrency.

Rust Prerequisites: 

Syntax: Rust has a concrete syntax similar to C and C++. The blocks of code are delimited by curly brackets, and control flow keywords such as if, else, while and for. Also, there are some new keywords are also introduced such as match(for pattern matching).

Memory safety: Rust is designed to be memory safe and it does not allow null pointers, dangling pointers, or data safe in safe code. Rust core library also provides an option type.

Memory Management: In Rust, there is no automated garbage collection like  Java and other languages.

Hello World Example:

First, we will create a file named main.rs(.rs  is the extension for Rust). And then will write the following the below code.

fn main(){
println!(“Hello World, Welcome to Rust”);
}

Now we will run this code snippet with the following command:

$ rustc main.rs
$ ./main

Output:
Hello World, Welcome to Rust

Above is  hello world code in rust, now we will see what just happen in your “Hello World, Welcome to Rust” program in detail:

In the first line ‘fn’ defines a function in Rust, ‘main’ function is special as it is the beginning for every code in Rust. Here this function doesn’t return any value and also don’t take an argument.

In the second line ‘println!()’ is calling Rust macro, which is how metaprogramming is done in Rust. In Rust when we see ‘!’ means that that is a macro instead of normal function.

Variables In Rust:

In Rust, we use variable binding for assigning a value to a name. Like

let x = 20;

Here x is a variable which is assigned a value 20. But here let is a pattern, not a name i.e we can also use pattern in this way:

let (x, y) = (3, 4);

This means x is 3 and y is 4.
By default, a variable is immutable in Rust. That means in, let x = 20;  x is immutable and if we need to make the variable mutable we have to use the mut keyword as explained below

let mut x = 20;  // here x is mutable.



There are many features in Rust which can’t be shared on a single blog so we will be discussing that in my future blogs.

References:

https://en.wikipedia.org/wiki/Rust_(programming_language)
https://doc.rust-lang.org/book/second-edition/index.html

knoldus-advt-sticker

Written by 

Ayush Aggarwal is a Software Consultant having experience of more than 11 months. He has knowledge of various programming languages such as JAVA, C and C++, and also has good knowledge of database technologies like MySQL. He has worked upon Object Oriented Paradigm and a Java and Android enthusiast. His hobbies include playing football, basketball etc.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading