Skip to content

Latest commit

 

History

History
175 lines (153 loc) · 4.44 KB

File metadata and controls

175 lines (153 loc) · 4.44 KB

TRust

This document outlines features of rust programming lanuage.

Table of Contents (Optional)

Variables Consts

Reference from official documentation

Variables

General syntax for declaration, assigning.

let identifier = expression ;
let mut identifier = expression ;
let identifier : type = expression ;
let mut identifier : type = expression ;
let mut identifier ;
let mut identifier : type ;
identifier = expression;

let ILP = "Introduction to lanuage processing";
let mut grade = "A";
let marks : i32 = 100;
let mut sq2 = square(2)*2;
let mut notAssigned;
let mut vartype:i32;

//Some other valid examples
let d = true;
let e = !d;
e = !!e;
let f = (true || d) && (a && b) || false && true;
let c = 5 * a;
c = c++;
c += d;
c = this.something;
let mut k = 10;
a = area(area(area(a,area(area(a,b)*area(a,b),b)),b),a);
a = area(a*3,b*7);
a = area(10,b%5)%10;

Constants

General syntax for declaration, assigning.

const identifier = expression;
const identifier : type = expression;

const con = "test";
const cont : i32 = 5;

Structs

Reference from official documentation

syntax for declaration

struct identifier { identifier:type,identifier:type,..}

struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}

syntax for using

let mut user1 = User {
    email: String::from("[email protected]"),
    username: String::from("someusername123"),
    active: true,
    sign_in_count: 1,
};

user1.email = String::from("[email protected]");

Vectors

syntax

//defining vector
let v = vec![1, 2, 3, 4, 5]; 
// accessing value
val  = v[10];

Functions

Reference from official documentation
main is the entry point function, all valid rust programs should have main function. The syntax analyser checks for valid defination of main. The main function cannot be called by user.

Syntax for declaration of functions.

fn indentifier(){code}
fn indentifier(indetifer:type,indetifer:type){code}
fn indentifier()->returnType{code}
fn indentifier(indetifer:type,indetifer:type)->returnType{code}

fn another_function(x: i32)->i:32 {
    println!("The value of x is: {}", x);
    return x;
}

Syntax for calling of functions.

indentifier(params);
indetifier = indentifier();
let indetifier = indentifier();
indentifier();
indetifier = indentifier(params);
let indetifier = indentifier(params);

let area_sqaure10x20 = area(10,20);
a = area(area(area(a,area(area(a,b)*area(a,b),b)),b),a);
sqaure();

Conditionals

Reference from offical documentation

syntax

if expression { code }
if expression { code } else {code}
if expression { code } else if expression {code}
if expression { code } else if expression {code} else {code}

if number < 5 {
    println!("condition was true");
}

if number < (true || d) && (a && b) || false && true {
    println!("condition was true");
} else {
    println!("condition was false");
}

if number < 5 {
    println!("condition was true");
} else if number > 10 {
    println!("else if condition true");
} else {
    println!("condition was false");
}

Loops

Reference from offical documentation

for loop syntax

for variable start_int..stop_int{code}

for x in 0..10 {
    if x % 2 == 0 { continue; }
    if x == 8 {break;}
    println!("{}", x);
}

while loop syntax

while boolean_conditon {code}

let mut x = 5; // mut x: i32
let mut done = false; // mut done: bool

while !done {
    x += x - 3;

    println!("{}", x);

    if x % 5 == 0 {
        done = true;
    }
}