0
0
Rustprogramming~10 mins

Output using println macro in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print "Hello, world!" using println! macro.

Rust
fn main() {
    [1]!("Hello, world!");
}
Drag options to blanks, or click blank then click option'
Aprintln
Bprint
Cformat
Deprintln
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using print! instead of println! which does not add a new line.
Using format! which returns a string but does not print.
2fill in blank
medium

Complete the code to print the value of variable x using println! macro.

Rust
fn main() {
    let x = 5;
    println!("Value is: [1]", x);
}
Drag options to blanks, or click blank then click option'
A"x"
B"%d"
C"{x}"
D"{}"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using quotes around variable name inside the string.
Using C-style format specifiers like %d which are not valid in Rust.
3fill in blank
hard

Fix the error in the code to correctly print the sum of a and b.

Rust
fn main() {
    let a = 3;
    let b = 4;
    println!("Sum is: [1]", a + b);
}
Drag options to blanks, or click blank then click option'
A"{a + b}"
B"a + b"
C"{}"
D"sum"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Putting the expression inside quotes so it prints literally.
Using variable names inside braces without formatting.
4fill in blank
hard

Fill both blanks to print the name and age variables using println! macro.

Rust
fn main() {
    let name = "Alice";
    let age = 30;
    println!("Name: [1], Age: [2]", name, age);
}
Drag options to blanks, or click blank then click option'
A"{}"
B"{name}"
D"{age}"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using variable names inside the string placeholders instead of {}.
Using only one placeholder for two variables.
5fill in blank
hard

Fill all three blanks to print the uppercase name, age, and a greeting message.

Rust
fn main() {
    let name = "Bob";
    let age = 25;
    println!("[1] is [2] years old. [3]!", name.to_uppercase(), age, "Hello");
}
Drag options to blanks, or click blank then click option'
A"{}"
D"{name}"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using variable names inside the string placeholders instead of {}.
Mismatching the number of placeholders and arguments.