Look at this Rust program. What will it print?
fn main() {
let x = 5;
let y = 10;
let sum = x + y;
println!("Sum is: {}", sum);
}Variables store values so you can use them later in calculations.
The variables x and y hold numbers 5 and 10. Adding them gives 15, which is printed.
Choose the best reason why variables are important in programming.
Think about how you keep track of things in real life by giving them names.
Variables let us store data with a name so we can use or change it anytime in the program.
Consider this Rust code snippet. What will happen when you try to compile it?
fn main() {
println!("Value is: {}", value);
let value = 10;
}In Rust, variables must be declared before use.
The variable value is used before it is declared, causing a compile-time error.
What is wrong with this Rust code snippet?
fn main() {
let mut count = 5;
count = "ten";
println!("Count is {}", count);
}Rust variables have fixed types unless explicitly changed.
The variable count was declared as an integer but assigned a string later, causing a type mismatch error.
Count the number of variables declared and used in this Rust code.
fn main() {
let a = 3;
let b = 4;
let c = a * b;
let d = c + a;
println!("Result: {}", d);
}Count each let statement that creates a variable.
Variables a, b, c, and d are declared and used.