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 {:?} formatter:
The {:?} formatter is called the debug formatter. It is commonly used when you want to quickly inspect the contents of a variable — especially arrays, vectors, tuples, or structs — without writing extra code. It is great for debugging your program to see what values are stored inside a collection.
let numbers = [1, 2, 3, 4, 5];
print!("{:?} ", numbers);
// Output: [1, 2, 3, 4, 5]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.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