Better Print
Part of the Fundamentals section of Coddy's Rust journey — lesson 64 of 75.
We learned how to print an array using a for loop:
let numbers = [1, 2, 3, 4, 5];
for num in numbers {
print!("{} ", num);
}But there is a simpler way using the {:?} sign:
let numbers = [1, 2, 3, 4, 5];
print!("{:?} ", numbers);
// Output: [1, 2, 3, 4, 5]{:?} is the debug formatter in Rust. It is commonly used to quickly inspect the contents of arrays, structs, and other data types without writing a custom print loop — especially useful during development and debugging.
Another way it to use with precision (useful for floating-point arrays):
let arr = [1.12345, 2.23456, 3.34567];
println!("{:.2?}", arr);
// Output: [1.12, 2.23, 3.35]It will also work for simple numbers:
let num = 6.1234;
println!("{:.2?}", num);
// Output: 6.12Cheat sheet
Print arrays using {:?} format specifier:
let numbers = [1, 2, 3, 4, 5];
print!("{:?}", numbers);
// Output: [1, 2, 3, 4, 5]Use precision formatting with {:.n?} for floating-point numbers:
let arr = [1.12345, 2.23456, 3.34567];
println!("{:.2?}", arr);
// Output: [1.12, 2.23, 3.35]
let num = 6.1234;
println!("{:.2?}", num);
// Output: 6.12Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsString Comparison13Arrays Continued
Mutable Reference ArraysArray MethodsRecap - Product ArrayRecap - Reversed ArrayBetter Print5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 33Variables Part 2
Type DeclarationNaming ConventionsType InferenceRecap - Initialize VariablesType Casting9Loops
For Over SeriesWhile LoopBreakContinueNested LoopLoop LabelsInfinite LoopRecap - Dynamic Input