avatarAmit Chauhan
# Summary

The provided web content is an introductory tutorial on the Rust programming language, covering fundamental concepts such as data types, variables, constants, strings, operators, decision-making, loops, and functions.

# Abstract

This tutorial, titled "RUST: Zero to Hero Basic Introduction in a New Programming Language (Part 1/3)," serves as a primer for individuals looking to gain proficiency in Rust, a language designed for high performance and safety, particularly in systems programming. It outlines the initial steps to get started with Rust, including its installation process on Windows systems and an overview of Rust's syntax for defining variables, constants, and strings. The content delves into various operators, such as arithmetic, assignment, relational, logical, and bitwise, and how they are used in calculations and decision-making within Rust. Additionally, the tutorial explains the usage of control structures like `if`, `if-else`, and `for` loops to manage the flow of a program. The article concludes with a brief discussion on organizing code into functions to handle complex tasks, suggesting that Rust is advantageous in scenarios demanding both speed and safety. The author encourages readers to engage with them on LinkedIn and Twitter for further discussion.

# Opinions

- The author believes Rust to be a well-suited language for scenarios where speed and control over memory are critical due to its performance and safety features.
- It is implied that Rust is a valuable addition to the programming landscape, considering its unique benefits that other languages may not offer, as suggested by its inclusion in the tutorial's title as a "New Programming Language."
- The tutorial is positioned as a comprehensive guide aimed at beginners, starting from the basics to more advanced topics, ensuring a structured learning path.
- The author highlights the importance of understanding and correctly using Rust's type system, memory safety features, and other language constructs to develop efficient and secure applications.
- By providing detailed examples and encouraging further learning through recommended articles and engagement on social media, the author conveys a dedication to fostering a community of Rust enthusiasts and learners.

Programming

RUST: Zero to Hero Basic Introduction in a New Programming Language (Part 1/3)

A well-suited language in safety and control

Photo by Cris DiNoto on Unsplash

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 type

Variables 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: 4

String

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) => 2

Decision 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 10

Section 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.
  1. Defining a function
  2. 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.

Recommended Articles

  1. NLP — Zero to Hero with Python

2. Python Data Structures Data-types and Objects

3. MySQL: Zero to Hero

Programming
Analytics
Application Development
Data Science
Language
Recommended from ReadMedium