Programming
RUST: Zero to Hero Basic Introduction in a New Programming Language (Part 1/3)
A well-suited language in safety and control
Topics to be covered:
Section 1: Rust Introduction and Installation Guide
Section 2: Data Types, Variables, Constants, and String
Section 3: Operators and Loops
Section 4: Functions
Section 1:
Rust Introduction
Rust programming language initiated and developed by Mozilla. The need for other languages is to be more fast and secure. RUST gives very good performance at runtime and safety in memory issues.
Windows Installation of RUST
You can do programming on visual studio 2013 and later.
Download RUST 64bit — Link
After installing rust, open the cmd with admin to check it connects with the environment variable with rustccommand.
Section 2:
Data Types
Character type
Char is used in a programming language to have character type values to the variables.
let x = 'a';
let y = 'w';Integer type
This type is used in a programming language to have integer type values to the variables. The integers are differentiated with signed(i) and unsigned(u) with size ranges from 8bit to 128bit.
let a = 3;
let b = 6;
let c = 2143;Floating type
Floating types are those values with the decimal in it.
let x = 2.3;
let y = 1234.12;Bool type
Boolean types are standard types in which we get only two results, either True or False.
let yes = True;
Let no = False;Variables
The role of a variable in any programming variable is the same. A name is given to all assigned values.
The variables can have numbers, character, underscore. It can either start with a letter or underscore. ‘SUM’ and ‘sum’ are two different variables means it is case sensitive. Variable can be defined in two different ways, one is by without type and the second one is by type.
let a = 40; //without type
let a:i16 = 40.45; //with typeVariables values are immutable, and it means we can not change the values once it is assigned.
Constant
Constant means we define the specific value of the constant variable throughout the program, and it can not be changed. To define the constant use of a keyword const. We have to specify the type in the constant. Otherwise, it will give the error.
const PI:f8 = 3.14;
const AREA:f16 = 900;
let uname="Amit";
let uname= uname.len();
println!("integer length of character : {}",uname);
//output:
integer length of character: 4String
Strings in rust are divided into two types.
String Literal (&str)
This type of string is issued when the string value is known at compile time. It is a type of slice and points to UTF-8.
let home_name:&str="HAPPYHOME";String Object
This type is a part of the standard library and not a part of a language. The string in this is stored as bytes.
let new_string_obj = String::new();There are many string objects with their working functions.
- new() — it creates an empty string.
- replace() — replaces a string with pattern matches.
- push() — appends char in the defined string.
Section 3:
Operators
Operators in rust are of various types to do arithmetic and computation processes. Types of the operator are shown below:
- Arithmetic Operator
Example:
let x = 2;
let y = 3;
println!("Sum: {}",x+y);
println!("Difference: {}",x-y);
println!("Mul: {}",x*y);
println!("Divide: {}",x/y);
//output : Sum:5
//output : Difference:-1
//output : Mul:6
//output : Divide:0.6666- Assignment Operator
Example:
let x = 8;
let x = x+2;
println!("x: {}", x) ; //output : x:10
println!("x: {}",x += 2); //output : x:12
println!("x: {}",x *= 2); //output : x:24- Relational Operator
Example:
let x = 5;
let y = 6;
println!(x<y); //output : True
println!(x>y); //output : False
for comparison we use double equal sign '=='
println!(x=y) ; //output : False
println!(x<=y); //output : True
println!(x>=y); //output : True
println!(x!=y); //output : True- Logical Operator
Example:
let x = 5;
let y = 4;
if(x<8) && (y<5)
{
println!("True");
}
if(x<8) || (y>5)
{
println!("True");
}
if x!=6
{
println!("True");
} - Bitwise Operator
Bitwise operators are used to doing bitwise calculations on integers.
Example:
let x:i32 = 12;
let y:i32 = 13;
let a:i32 = 10;
let b:i32 = 2;
let output:i32;
output = x & y;
println!("(x & y) => {} ",output);
output = x | y;
println!("(x | y) => {} ",output);
output = x ^ y;
println!("(x ^ y) => {} ",output);
output = a << b;
println!("(a << b) => {}",output);
output = a >> b;
println!("(a >> b) => {}",output);
//output:
(a & b) => 12
(a | b) => 12
(a ^ b) => 1
(a << b) => 40
(a >> b) => 2Decision making and Loops
if statement
Control statements are used to execute statements base on conditions given to them. If the condition is true, then it will execute the if part otherwise, not.
let a:i32 = 3;
if a> 0
{
println!("number is greater than zero");
}if-else statement
Control statements are used to execute statements base on conditions given to them. If the condition is true, then it will execute the if part; otherwise, the else part will be executed.
let a = 10;
if num % 2==0
{
println!("Even");
}
else
{
println!("Odd");
}For loop
It is used for sequence. The for loop executes the block a defined number of times.
for x in 1..11 // 11 is not inclusive
{
if x==4
{
continue;
}
println!("x is {}",x);
}
//output:
x is 1
x is 2
x is 3
x is 5
x is 6
x is 7
x is 8
x is 9
x is 10Section 4:
Functions
Functions
- When we are working on a big project, we can break into smaller tasks, and then we use functions.
- Two main things we have to know.
- Defining a function
- Calling a function
In RUST, the function is defined by the fn keyword.
Syntax
fn function_name(arg1,arg2..argN)
{
// statements
}Example:
fn main()
{
//calling a function
sum();
}
//Defining a function
fn sum()
{
println!("The sum of the two numbers: ");
}Conclusion:
The RUST language is new and very useful in many areas where speed and safety are more important.
I hope you like the article. Reach me on my LinkedIn and twitter.






