In last 2 decades the computers and internet has made an increasing demand, and with evolution of new technologies, devices and protocols the programming languages also being updated regularly but still most of the early programming languages like C, C++ have shown some drawbacks. These drawbacks motivated others to create new programming languages like Go, Rust, Python and more. In these tutorials we will talk about one of these programming languages. Rust language is intended for highly concurrent and highly safe system. Rust language has an emphasis on safety, control of memory layout and concurrency.
Rust Programming Language
Rust is a multi-paradigm programming language like C++ syntax was designed for performance and safety, especially safe concurrency by using a borrow checker and ownership to validate references. Rust was developed by Graydon Hoare at Mozilla research with contributions from Dave Herman, Brendan Eich and others. Which achieves memory safety without garbage collection. Rust is a compiled system programming language.
Rust has many reasons for being popular among programmers. Below are the reasons are :
- Rust is Fast: Rust Programming Language is a multi-paradigm programming language similar to C++ syntax. Thus it becomes very easy to learn Rust for anyone. Across multiple platforms Rust code compiles to native machine code.
- Rust is Memory Safe: Rust inspires developer to write safe code. Unlike C, it does not provide memory unsafe thing like dangling pointer, uninitialized pointer and NULL pointer.
- Rust is Low-Overhead: In Rust Programming Language all values have a unique owner, and the scope of the value is same as scope of the owner That’s why It has an ownership system.
- Rust is flexible: Rust is designed for performance and safety, especially safe concurrency by using a borrow checker and ownership to validate references.
- Rust is easy to use: Rust Programming Language syntax is similar to C++ language syntax so it is easy to use or easy to understand.
An example program in rust, saved with extension .rs
geeks for geeks
We can Install Rust by using terminal. For linux and macOS open our terminal and use the curl which can automatically install rust for us, we can refer Rust docs for windows installation
$ curl –proto ‘=https’ –tlsv1.2 https://sh.rustup.rs -sSf | sh
We can check whether we have Rust installed correctly, open a shell and enter this command
$ rustc –version
Features of Rust Programming Language
Rust Programming Language creating and maintaining boundaries that preserve large-system integrity. Rust is a multi-paradigm programming language. it is designed for safety and performance. There are some features which makes it different.
- Ownership: In Rust Programming Language all values have a unique owner, and the scope of the value is same as scope of the owner That’s why It has an ownership system. Values can be passed by immutable reference and mutable reference, using &T and &mut T, or by value, using T. there are either be multiple immutable references or one mutable reference.
- Memory Safety: When it comes to memory safety, Rust inspires developer to write safe code. Unlike C, it does not provide memory unsafe thing like dangling pointer, uninitialized pointer and NULL pointer. Which in result, code becomes more safe and stable. It has define format to initialize data value. And similar to C, it does provide control to handle lifetime of a variable through added syntax. Apart from this it also provide flexibility to write unsafe code with unsafe keyword which Ideally should be avoided until there is no other way.
- Memory Management: A programmer’s performance also depends on how language manage memory internally. Rust works on RAII , unlike java’s garbage collection. Add on to this reference counting is also available to developer but that is optional.
What is Cargo?
You can check cargo from the below command, if you don’t see version number that means you haven’t installed cargo
$ cargo –version
We can create a new rust project using cargo, for that use the below commands.
cargo new gfg
cargo new command creates a new cargo project in the specified directory. the directory contains, cargo.lock, cargo.toml, src files of the project.
- cargo.lock – lock file for the project
- cargo.toml – contains details and dependencies of the project, am example file is shown below.
name = “gfg”
version = “0.1.0”
authors = [“Your Name <firstname.lastname@example.org>”]
edition = “2018”
- src – directory containing the source files of the project, main.rs file is main file for the project which will be created by default
We can run the project using any of the below commands.
// compiled out put
// runs the compiled output
// check the output