-
Notifications
You must be signed in to change notification settings - Fork 0
/
varable1.rs
98 lines (65 loc) · 1.7 KB
/
varable1.rs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// x and y were declared but not initialised any value at first
// I solved the problem by assigning x and y a value which must be equal so as to get a suuccess as our output
fn main() {
let x: i32 = 5;
let y: i32 = 1+4;
assert_eq!(x, y);
// assert_eq! is used to check equality of two expressions
println!("Success!");
//second quiz
//mut is used for reassignment
fn main() {
let mut x = 1;
//x+2 adds one to the value of x
x += 2;
assert_eq!(x, 3);
println!("Success!");
}
//third quiz(scope)
//y was not able to be accesed by the statement since it was inside it,
fn main() {
let x: i32 = 10;
let y: i32 = 5;
{
println!("The value of x is {} and value of y is {}", x, y);
}
println!("The value of x is {} and value of y is {}", x, y);
}
//shadowing
// made sure that when calling assert_eq! values should be the same
fn main() {
let x: i32 = 5;
{
let x = 12;
assert_eq!(x, 12);
}
assert_eq!(x, 5);
let x = 42;
println!("{}", x); // Prints "42".
}
//shadowing
fn main() {
let mut x: i32 = 1;
x = 7;
// Shadowing and re-binding
x += 3;
let y = 4;
// Shadowing
let y = "I can also be bound to text!";
println!("Success!");
}
//unused variable problem
fn main() {
let x = 1;
println! ("{}", x);
}
//destructuring problem
fn main() {
let (mut x, mut y) = (1, 3);
x += 2;
assert_eq!(x, 3);
//making the value of y to be 2
y -=1;
assert_eq!(y, 2);
println!("Success!");
}