This document outlines features of rust programming lanuage.
- [Variables and Consts](#Variables\ Consts)
- Structures
- Vectors
- Functions
- Conditionals
- Loops
Reference from official documentation
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;const identifier = expression;
const identifier : type = expression;
const con = "test";
const cont : i32 = 5;Reference from official documentation
struct identifier { identifier:type,identifier:type,..}
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}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]");//defining vector
let v = vec![1, 2, 3, 4, 5];
// accessing value
val = v[10];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.
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;
}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();Reference from offical documentation
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");
}Reference from offical documentation
for variable start_int..stop_int{code}
for x in 0..10 {
if x % 2 == 0 { continue; }
if x == 8 {break;}
println!("{}", x);
}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;
}
}