0
0
Rustprogramming~10 mins

Formatting output 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 the value of x using Rust's formatting macro.

Rust
let x = 10;
println!("[1]", x);
Drag options to blanks, or click blank then click option'
A"{}"
B"{:?}"
C"{x}"
D"%d"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using %d like in C language.
Using {x} which is not valid in Rust formatting.
Using debug format {:?} when simple display is enough.
2fill in blank
medium

Complete the code to print the variable name with a greeting using Rust formatting.

Rust
let name = "Alice";
println!("Hello, [1]!", name);
Drag options to blanks, or click blank then click option'
A"name"
B"{:?}"
C"{}"
D"{name}"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Putting the variable name inside quotes, which prints the literal text.
Using debug format {:?} unnecessarily.
Trying to use {name} without the new Rust feature for named arguments.
3fill in blank
hard

Fix the error in the code to correctly print the floating-point number with 2 decimal places.

Rust
let pi = 3.14159;
println!("Pi is approximately [1]", pi);
Drag options to blanks, or click blank then click option'
A"{:.2f}"
B"{:.2}"
C"{:2}"
D"{:.2p}"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using {:.2f} which is invalid in Rust.
Using {:2} which sets width, not decimal places.
Using {:.2p} which is not a valid format specifier.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Rust
let words = ["apple", "cat", "banana", "dog"];
let lengths = words.iter().filter(|&&word| word.len() [1] 3).map(|&word| (word, word.len())).collect::<std::collections::HashMap<_, _>>();
Drag options to blanks, or click blank then click option'
A!=
B<
C==
D>
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using < which keeps shorter words.
Using == which keeps only words of length exactly 3.
Using != which keeps all words except length 3.
5fill in blank
hard

Fill all three blanks to create a formatted string that shows the name in uppercase, age, and a check if age is over 18.

Rust
let name = "bob";
let age = 20;
let info = format!("Name: [1], Age: [2], Adult: [3]", name.[1](), age, age [3] 18);
Drag options to blanks, or click blank then click option'
Ato_uppercase
Bage
C>
Dto_lowercase
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using to_lowercase() instead of uppercase.
Using < instead of > for age comparison.
Not calling the method with parentheses.