-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.rs
128 lines (96 loc) · 1.99 KB
/
variables.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
1)
// Fix the error below with least amount of modification to the code
fn main() {
let x: i32 = 5; // Uninitialized but used, ERROR !
let y: i32; // Uninitialized but also unused, only a Warning !
assert_eq!(x, 5);
println!("Success!");
}
// Success!
2)
// Fill the blanks in the code to make it compile
fn main() {
let mut x = 1;
x += 2;
assert_eq!(x, 3);
println!("Success!");
}
// Success!
3)
// Fix the error below with least amount of modification
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);
}
// The value of x is 10 and value of y is 5
The value of x is 10 and value of y is 5
4)
// Fix the error with the use of define_x
fn main() {
let x = define_x();
println!("{}, world", x);
}
fn define_x() -> String {
let x = "hello".to_string();
x
}
// hello, world
5)
// Only modify `assert_eq!` to make the `println!` work(print `42` in terminal)
fn main() {
let x: i32 = 5;
{
let x = 12;
assert_eq!(x, 12);
}
assert_eq!(x, 5);
let x = 42;
println!("{}", x); // Prints "42".
}
// 42
6)
// Remove a line in the code to make it compile
fn main() {
let mut x: i32 = 1;
x = 7;
// Shadowing and re-binding
let x = x;
// x += 3;
let y = 4;
// Shadowing
let y = "I can also be bound to text!";
println!("Success!");
}
// Success!
7)
//UNUSED VARIABLES
fn main() {
let x = 1;
println!("{}",x)
}
// Warning: unused variable: `x`
// 1
8)
// Fix the error below with least amount of modification
fn main() {
let (mut x, y) = (1, 2);
x += 2;
assert_eq!(x, 3);
assert_eq!(y, 2);
println!("Success!");
}
// Success!
9)
fn main() {
let (x, y);
(x,..) = (3, 4);
[.., y] = [1, 2];
// Fill the blank to make the code work
assert_eq!([x,y], [3,2]);
println!("Success!");
}
//Success!