Rust

Aditi Dosi
Dev Genius
Published in
4 min readAug 26, 2022

--

Rust is a statically-typed programming language designed for performance and safety, especially safe concurrency and memory management.

Ever since the Rust Foundation released the popular programming language, it has been adopted by many industry leaders, including Mozilla, Dropbox, Google, Microsoft, and Amazon.

Syntax:

fn functionname(arguments){
code
}

Is Rust a frontend or backend?

Rust provides various web frameworks for backend development, including a combination of tools, helpers, and libraries from different vendors that are each designed to provide a way to build, test, and run apps with efficiency, security, and flexibility.

Is Rust better than Java?

When it comes to Java, this programming language is significantly slower than Rust. Rust delivers faster startup times and smaller memory footprint on top of it. Java uses Garbage Collection for memory management, which decreases performance.

Concept of Ownership:

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.

  • Rust

fn helper() -> Box<int> {

let three = box 3;

return three; //transfer ownership

}

fn main() {

// acquire ownership of return value

let my_three = helper();

}

Concept of Borrowing:

Owned 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.

  • Rust

let a: &int;

{

let b =3; //b lifetime is not same as a

a =&b;

} //

Here “a” and “b” has a different lifetime, so it will not work.

  • Rust

let a: &int;

let b=3; // a and b have same lifetime

a = &b;

Here “a” and “b” have the same life, so it will work. Borrow can be nested. Through cloning, borrowed values can become owned.

Memory management in Rust:

  1. Rust has fine-grain memory management but is automatically managed once created.
  2. In Rust when you allocate memory you never have to really free it you can decide when to free it but never call it. Rust takes care of it automatically.
  3. Each variable has a scope it is valid for, and it gets automatically deallocated once it goes out of scope.
  4. In rust, each program is allocated memory from the operating system.
  5. Rust also has a shared memory where we can have a reference piece of data, we can use ownership to keep track of reference count.

Different memory :

Heap:

  • It is the biggest memory block and is managed by the rust ownership model.
  • At this place, all the dynamic data is stored.

Stack:

  • All values in rust are allocated on the stack.
  • At this, the static memory is allocated by default.
  • There is one stack per thread.
  • It includes structures and pointers to dynamic data.

Mutability:

Values in rust are immutable by default and must be tagged as being mutable(if needed).

Example:

  • Rust

let x = 2;

x = 9; //it will show an error

The above example will show an error because we have not tagged it as mutable.

  • Rust

let mut x = 2;

x = 9; //work correctly

This will work fine as we have tagged it as being mutable. As in this case we are explicitly mutating it.

Structure in Rust

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
}

Example:

  • Rust

struct Employee {

name: String,

company: String,

employee_id: u32,

profile: String

}

fn main() {

let value = Employee {

name: String::from("Geek"),

company: String::from("Geeksforgeeks.org"),

employee_id: 007,

profile:String::from("Manager"),

};

println!("Employee: {} of {} Company bearing EmployeeID {} is of {} level.",

value.name,

value.company,

value.employee_id,

value.profile);

}

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.

Tuple:

A tuple in rust is a finite heterogeneous compound data type, 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.

Tuples in Rust are defined using small brackets as shown below :

Syntax: ("geeksforgeeks", 1, 'geek')

Example:

  • Rust

// Rust program to get value from tuple

// using index

fn main() {

let gfg = ("cp", "algo", "FAANG", "Data Structure");

// complete tuple

println!("complete tuple = {:?} ", gfg );

// first value

println!("at 0 index = {} ", gfg.0 );

// second value

println!("at 0 index = {} ", gfg.1 );

// third value

println!("at 0 index = {} ", gfg.2 );

// fourth value

println!("at 0 index = {} ", gfg.3 );

}

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

Rust Type System:

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:

Programming languages: Rust just got a big boost from Facebook’s Meta

Rust, initially developed within Mozilla, is joining Meta as an officially endorsed server-side language. Adding Rust as a supported language

--

--