Challenge - 5 Problems
Rust println Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
What is the output of this Rust code using println macro?
Consider the following Rust code snippet. What will it print when run?
Rust
fn main() {
let x = 5;
println!("The value of x is: {}", x);
}Attempts:
2 left
๐ก Hint
Look at how the placeholder {} is replaced by the variable x.
โ Incorrect
The println! macro replaces {} with the value of x, which is 5, so it prints exactly: The value of x is: 5
โ Predict Output
intermediate2:00remaining
What does this println macro print with multiple placeholders?
What is the output of this Rust code?
Rust
fn main() {
let a = 3;
let b = 7;
println!("Sum of {} and {} is {}", a, b, a + b);
}Attempts:
2 left
๐ก Hint
Each {} is replaced by the corresponding argument in order.
โ Incorrect
The placeholders {} are replaced by a, b, and a+b respectively, so the output is: Sum of 3 and 7 is 10
โ Predict Output
advanced2:00remaining
What is the output of this Rust code with named arguments in println?
Look at this Rust code using named arguments in println! macro. What will it print?
Rust
fn main() {
let name = "Alice";
let age = 30;
println!("Name: {person}, Age: {years}", person=name, years=age);
}Attempts:
2 left
๐ก Hint
Named arguments replace placeholders with matching names.
โ Incorrect
The placeholders {person} and {years} are replaced by the values of name and age variables, so it prints: Name: Alice, Age: 30
โ Predict Output
advanced2:00remaining
What error does this Rust code produce when using println macro incorrectly?
What error will this Rust code produce?
Rust
fn main() {
let x = 10;
println!("Value is: {}", );
}Attempts:
2 left
๐ก Hint
Check if all placeholders have matching arguments.
โ Incorrect
The println! macro expects an argument for the {} placeholder, but none is provided, causing a syntax error: expected expression, found `)`
๐ง Conceptual
expert2:00remaining
How many items are printed by this Rust code using println! in a loop?
How many lines will this Rust program print?
Rust
fn main() {
for i in 1..=5 {
println!("Number: {}", i);
}
}Attempts:
2 left
๐ก Hint
The range 1..=5 includes numbers from 1 to 5 inclusive.
โ Incorrect
The loop runs from 1 to 5 inclusive, so it prints 5 lines, one for each number.