-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvariable.rs
More file actions
67 lines (54 loc) · 1.39 KB
/
variable.rs
File metadata and controls
67 lines (54 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::mem::size_of_val;
fn main(){
println!("---------Main function----------");
let x: i32=5;
assert_eq!(x,5); //checking the value, if not true then it will terminate
println!("Success");
println!();
//mutable declaration
mutable();
//shadowing the variable
shadowing();
//range function
println!("---------Scope Declaration----------");
{
let y:i32=50;
let mut x:i32 =x; //we can redeclare variable inside scope with same name
x=x+30; //all the computation of that varibale will only be in that particular scope
println!("The value of y and x will be {} {}",y,x);
}
println!("{}",x); //prints 5
println!();
//other data types
char();
bool();
}
fn mutable(){
println!("---------Mutable function----------");
let mut y: i32=10;
y=y+20;
println!("{}",y); //we need to create block for varible printing
println!();
}
fn shadowing(){
println!("---------Shadowing the variable----------");
let mut x:i32=20;
x=x+9;
let x:i32=10;
println!("After shadowing, value of x will be {}",x);
println!();
}
fn char(){
println!("---------Character----------");
let c:char='a';
println!("{}",size_of_val(&c));
println!();
}
fn bool(){
println!("---------Boolean----------");
let b:bool=true;
if b{
println!("True");
}
println!();
}