Look at this Rust code. What will it print when run?
fn main() {
let x = 5;
let y = 10;
println!("Sum is: {}", x + y);
}Remember, the + operator adds numbers, not strings.
The variables x and y hold numbers 5 and 10. Adding them gives 15. The println! macro prints "Sum is: 15".
Check this Rust code. What is the output?
fn main() {
let name = "Alice";
println!("Hello, {}!", name);
}The curly braces {} are replaced by the variable value.
The println! macro replaces {} with the value of name, which is "Alice".
What will this Rust program print?
fn main() {
let number = 7;
if number % 2 == 0 {
println!("Even");
} else {
println!("Odd");
}
}Check if 7 is divisible by 2 without remainder.
7 % 2 equals 1, so the else branch runs, printing "Odd".
Look at this Rust code. What will it print?
fn main() {
let mut count = 0;
while count < 3 {
println!("Count: {}", count);
count += 1;
}
}The loop runs while count is less than 3, starting from 0.
The loop prints count values 0, 1, and 2, then stops when count reaches 3.
What will this Rust program print?
fn greet(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
let user = String::from("Bob");
greet(&user);
}Check how the string is passed to the function.
The function greet takes a string slice (&str). Passing &user works fine and prints "Hello, Bob!".