The following program works perfectly:
fn main() {
const FAVORITE_NUMBER: i32 = 42;
const FAVORITE_COLOR: &str = "blue";
const HOBBIES: [&str; 3] = ["Programming", "Video Games", "Reading"];
println!("Favorite number is {FAVORITE_NUMBER}, favorite color is: {FAVORITE_COLOR}, hobbies are: {:?}", HOBBIES);
}
However for the sake of consistency, I would like to be able to print the array of strings inline, similar to the strings and ints, e.g. something like:
println!("Favorite number is {FAVORITE_NUMBER}, favorite color is: {FAVORITE_COLOR}, hobbies are: {:HOBBIES}";
This is a syntax error, but serves to illustrate the concept. I have had similar issues when printing vectors and structs.
How can I print an array, vec, or struct inline?
You can specify an identifier and format specifier like so:
println!("hobbies are: {HOBBIES:?}");