From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Formatting print statements

Formatting print statements - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Formatting print statements

- [Instructor] The print line macro is incredibly useful to display a string of information. And Rust has lots of formatting options to control how that information gets presented. So, let's look at some of those. The print line macro here on line five is currently using the default formatting. And when I run this program, we see that the floating point result of 10 divided by 3 is displayed as 3.333 repeating. Until the floating point reaches the limit of its precision. We can control the number of decimal places to show by including additional formatting information between the curly braces that get replaced by the value of c. This sequence of :.3 tells the print line macro to display the value with exactly three decimal places of precision. And when I run this program, we can see that's the result. But the string formatting options don't just end there. We can tell the formatter how many total character spaces we want…

Contents