Complete the code to print the value of x using Rust's formatting macro.
let x = 10; println!("[1]", x);
%d like in C language.{x} which is not valid in Rust formatting.{:?} when simple display is enough.Rust uses {} inside println! to format and print values.
Complete the code to print the variable name with a greeting using Rust formatting.
let name = "Alice"; println!("Hello, [1]!", name);
{:?} unnecessarily.{name} without the new Rust feature for named arguments.Use {} as a placeholder to insert the value of name in the string.
Fix the error in the code to correctly print the floating-point number with 2 decimal places.
let pi = 3.14159; println!("Pi is approximately [1]", pi);
{:.2f} which is invalid in Rust.{:2} which sets width, not decimal places.{:.2p} which is not a valid format specifier.Rust uses {:.2} to format a float with 2 decimal places. The f is not used.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
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<_, _>>();
< which keeps shorter words.== which keeps only words of length exactly 3.!= which keeps all words except length 3.The filter should keep words with length greater than 3, so use >.
Fill all three blanks to create a formatted string that shows the name in uppercase, age, and a check if age is over 18.
let name = "bob"; let age = 20; let info = format!("Name: [1], Age: [2], Adult: [3]", name.[1](), age, age [3] 18);
to_lowercase() instead of uppercase.< instead of > for age comparison.Use to_uppercase() to capitalize the name, print age, and check if age > 18 for adult status.