0
0
Rustprogramming~20 mins

Output using println macro in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust println Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2: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);
}
Ax = 5
BThe value of x is: x
CThe value of x is: {}
DThe value of x is: 5
Attempts:
2 left
๐Ÿ’ก Hint
Look at how the placeholder {} is replaced by the variable x.
โ“ Predict Output
intermediate
2: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);
}
ASum of a and b is a + b
BSum of 3 and 7 is 10
CSum of {} and {} is {}
DSum of 3 and 7 is a + b
Attempts:
2 left
๐Ÿ’ก Hint
Each {} is replaced by the corresponding argument in order.
โ“ Predict Output
advanced
2: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);
}
AName: {person}, Age: {years}
BName: name, Age: age
CName: Alice, Age: 30
DName: Alice Age: 30
Attempts:
2 left
๐Ÿ’ก Hint
Named arguments replace placeholders with matching names.
โ“ Predict Output
advanced
2: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: {}", );
}
Aerror: expected expression, found `)`
BValue is: 10
CValue is: {}
Derror: mismatched types
Attempts:
2 left
๐Ÿ’ก Hint
Check if all placeholders have matching arguments.
๐Ÿง  Conceptual
expert
2: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);
    }
}
A5
B4
C6
D1
Attempts:
2 left
๐Ÿ’ก Hint
The range 1..=5 includes numbers from 1 to 5 inclusive.