![]() |
VOOZH | about |
Rust is a blazing fast and memory-efficient static compiled language with a rich type system and ownership model. It can be used to power performance-critical services while guaranteeing memory-safety and thread-safety, empowering developers to debug at compile-time. In addition to that Rust has great documentation and a user-friendly compiler with top-end tools like integrated package managers and multi-editor with features like type inspection and auto-completion. Rust prevents all the crashes, and, interestingly, Rust is safe by default like JavaScript, Ruby, and Python. This is much more powerful than C/C++ because we cannot write the wrong parallel code you can never see fault in rust. It is very fast in representing a lot of programming paradigms very well.
👁 Introduction-to-Rust-Programming-LanguageBut the question arises as there are already so many programming languages like Python, C++, Java, etc, then why the need for developing a new programming language? The answer to this is the other programming languages have a lot of control over the hardware that you are running, like, you can optimize it well, translate directly to assembly code, but it's not very safe.
So rust provides us all the controls that we can have and all the levels of security that we can achieve.
Rust is using Rust which means that all the standard compiler libraries are written in rust; there is a bit of use of the C programming language but most of it is Rust. The big project of Rust is "servo", It is a project to write out the totally parallel layout engine like Gecko in Firefox or WebKit in Safari.
Servo built the layout engine, something to render HTML from bottom to top.
Table of Content
Functions are the block of reusable code that can perform similar related actions. You have already seen one of the most important functions in the language: the main function, which is the entry point of many programs. You’ve also seen the “fn” keyword, which allows you to declare new functions. Rust code uses snake case as the conventional style for function and variable names. In snake case, all letters are lowercase and underscore separate words.
Syntax:
fn functionname(arguments){
code
}
Here,
Example: The below function simply prints "Hello, World!" in the console:
Output:
Hello, World!The concept of ownership is that when you own something you can decide to pass it to someone else, if you have a book, and you have done reading it you can permanently give it to someone and not worry about it.
Below is the Rust program to implement the approach:
Output:
3Owned values in rust can be borrowed to allow usage for a certain period of time The "&" sign represents the borrowed reference. Borrowed values have a lifetime and are valid for that time only. Borrowing prevents moving. While there is an active borrow I can not transfer ownership. I still own it but cannot transfer it until I handed it in to really relinquish that borrowed.
Below is the Rust program to implement the approach:
Here "a" and "b" has a different lifetime, so it will not work.
Output:
3
3
Here "a" and "b" have the same life, so it will work. Borrow can be nested. Through cloning, borrowed values can become owned.
1. Heap:
2. Stack:
Values in rust are immutable by default and must be tagged as being mutable(if needed).
Below is the Rust program to implement Mutability:
The above example will show an error because we have not tagged it as mutable.
Output:
9This will work fine as we have tagged it as being mutable. As in this case we are explicitly mutating it.
The structure is a user-defined data type in rust that is used to combine different data items of a different type. Structure defines data as a key-value pair. The struct keyword is used to define Structures in Rust.
Syntax:
struct Name_of_structure {
field1:data_type,
field2:data_type,
field3:data_type
}
Below is the Rust program to implement structure:
Output:
Employee: Geek of Geeksforgeeks.org Company bearing EmployeeID 7 is of Manager level.This is an example of how we create structures in rust. This will compile perfectly.
A tuple in rust is a finite heterogeneous , meaning it can store more than one value at once. In tuples, there is no inbuilt method to add elements into a tuple. We can use the index to get the value of a tuple, and we also can not iterate over a tuple using for loop.
Syntax:
("geeksforgeeks", 1, 'geek')
Below is the Rust program to implement Tuple:
Output:
complete tuple = ("cp", "algo", "FAANG", "Data Structure")
at 0 index = cp
at 0 index = algo
at 0 index = FAANG
at 0 index = Data Structure
In Rust, every variable, value, and item has a type. The type defines how much memory it is holding and which operation can be performed on the value. The below table states all the types in Rust:
Type | Contents |
|---|---|
Primitive Types | Boolean, Numeric, Textual, Never |
Sequence Types | Tuple, Array, Slice |
User-defined Types | Struct, Enum, Union |
Function Types | Functions, Closures |
Pointer Types | References, Raw pointers, Function pointers |
Trait Types | Trait objects, Impl trait |