Complete the code to declare a variable inside the main function.
fn main() {
let [1] = 5;
println!("Value: {}", x);
}The variable name x is declared with let inside main. This makes it accessible within the function.
Complete the code to print the variable declared inside the block.
fn main() {
{
let y = 10;
println!("[1]: {}", y);
}
}The variable y is declared inside the block and can be used there. So we print y.
Complete the code to attempt accessing the variable outside its scope and observe the error.
fn main() {
{
let z = 15;
}
println!("Value: {}", [1]);
}z outside its block (this is expected to error).The variable z is out of scope outside the block, so using it causes a compile error: 'z not found in this scope'.
Fill both blanks to declare and use a variable inside a nested block.
fn main() {
let [1] = 20;
{
println!("[2] inside block: {}", [1]);
}
}The variable num is declared outside the nested block and is accessible inside it. The print label matches for clarity.
Fill all three blanks to declare a variable, declare another based on it, and print both values.
fn main() {
let [1] = 5;
let [2] = [1] + 10;
println!("Original: {}, Second: {}", [1], [2]);
}The variable x is declared first (=5). Then y is declared as x + 10 (=15). Both are in scope for printing.