1
1
# Formatted print
2
2
3
- Printing is handled by a series of [ ` macros ` ] [ macros ] defined in [ ` std::fmt ` ] [ fmt ]
4
- some of which include:
3
+ Printing is handled by a series of [ ` macros ` ] [ macros ] defined in
4
+ [ ` std::fmt ` ] [ fmt ] some of which include:
5
5
6
6
* ` format! ` : write formatted text to [ ` String ` ] [ string ]
7
- * ` print! ` : same as ` format! ` but the text is printed to the console (io::stdout).
7
+ * ` print! ` : same as ` format! ` but the text is printed to the console
8
+ (io::stdout).
8
9
* ` println! ` : same as ` print! ` but a newline is appended.
9
- * ` eprint! ` : same as ` print! ` but the text is printed to the standard error (io::stderr).
10
+ * ` eprint! ` : same as ` print! ` but the text is printed to the standard error
11
+ (io::stderr).
10
12
* ` eprintln! ` : same as ` eprint! ` but a newline is appended.
11
13
12
14
All parse text in the same fashion. As a plus, Rust checks formatting
@@ -20,7 +22,7 @@ fn main() {
20
22
21
23
// Positional arguments can be used. Specifying an integer inside `{}`
22
24
// determines which additional argument will be replaced. Arguments start
23
- // at 0 immediately after the format string
25
+ // at 0 immediately after the format string.
24
26
println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
25
27
26
28
// As can named arguments.
@@ -29,13 +31,13 @@ fn main() {
29
31
subject="the quick brown fox",
30
32
verb="jumps over");
31
33
32
- // Different formatting can be invoked by specifying the format character after a
33
- // `:`.
34
- println!("Base 10: {}", 69420); //69420
35
- println!("Base 2 (binary): {:b}", 69420); //10000111100101100
36
- println!("Base 8 (octal): {:o}", 69420); //207454
37
- println!("Base 16 (hexadecimal): {:x}", 69420); //10f2c
38
- println!("Base 16 (hexadecimal): {:X}", 69420); //10F2C
34
+ // Different formatting can be invoked by specifying the format character
35
+ // after a `:`.
36
+ println!("Base 10: {}", 69420); // 69420
37
+ println!("Base 2 (binary): {:b}", 69420); // 10000111100101100
38
+ println!("Base 8 (octal): {:o}", 69420); // 207454
39
+ println!("Base 16 (hexadecimal): {:x}", 69420); // 10f2c
40
+ println!("Base 16 (hexadecimal): {:X}", 69420); // 10F2C
39
41
40
42
41
43
// You can right-justify text with a specified width. This will
@@ -46,29 +48,28 @@ fn main() {
46
48
//and left-adjust by flipping the sign. This will output "10000".
47
49
println!("{number:0<5}", number=1);
48
50
49
- // You can use named arguments in the format specifier by appending a `$`
51
+ // You can use named arguments in the format specifier by appending a `$`.
50
52
println!("{number:0>width$}", number=1, width=5);
51
53
52
54
53
- // Rust even checks to make sure the correct number of arguments are
54
- // used.
55
+ // Rust even checks to make sure the correct number of arguments are used.
55
56
println!("My name is {0}, {1} {0}", "Bond");
56
57
// FIXME ^ Add the missing argument: "James"
57
58
58
59
// Only types that implement fmt::Display can be formatted with `{}`. User-
59
- // defined types do not implement fmt::Display by default
60
+ // defined types do not implement fmt::Display by default.
60
61
61
62
#[allow(dead_code)]
62
63
struct Structure(i32);
63
64
64
65
// This will not compile because `Structure` does not implement
65
- // fmt::Display
66
+ // fmt::Display.
66
67
//println!("This struct `{}` won't print...", Structure(3));
67
68
// TODO ^ Try uncommenting this line
68
69
69
70
// For Rust 1.58 and above, you can directly capture the argument from a
70
71
// surrounding variable. Just like the above, this will output
71
- // " 1". 4 white spaces and a "1".
72
+ // " 1", 4 white spaces and a "1".
72
73
let number: f64 = 1.0;
73
74
let width: usize = 5;
74
75
println!("{number:>width$}");
@@ -80,7 +81,7 @@ of text. The base form of two important ones are listed below:
80
81
81
82
* ` fmt::Debug ` : Uses the ` {:?} ` marker. Format text for debugging purposes.
82
83
* ` fmt::Display ` : Uses the ` {} ` marker. Format text in a more elegant, user
83
- friendly fashion.
84
+ friendly fashion.
84
85
85
86
Here, we used ` fmt::Display ` because the std library provides implementations
86
87
for these types. To print text for custom types, more steps are required.
@@ -90,14 +91,14 @@ Implementing the `fmt::Display` trait automatically implements the
90
91
91
92
### Activities
92
93
93
- * Fix the issue in the above code (see FIXME) so that it runs without
94
- error.
95
- * Try uncommenting the line that attempts to format the ` Structure ` struct (see TODO)
96
- * Add a ` println! ` macro call that prints: ` Pi is roughly 3.142 ` by controlling
97
- the number of decimal places shown. For the purposes of this exercise,
98
- use ` let pi = 3.141592 ` as an estimate for pi. (Hint: you may need to
99
- check the [ ` std::fmt ` ] [ fmt ] documentation for setting the number of
100
- decimals to display)
94
+ * Fix the issue in the above code (see FIXME) so that it runs without
95
+ error.
96
+ * Try uncommenting the line that attempts to format the ` Structure ` struct
97
+ (see TODO)
98
+ * Add a ` println! ` macro call that prints: ` Pi is roughly 3.142 ` by controlling
99
+ the number of decimal places shown. For the purposes of this exercise, use
100
+ ` let pi = 3.141592 ` as an estimate for pi. (Hint: you may need to check the
101
+ [ ` std::fmt ` ] [ fmt ] documentation for setting the number of decimals to display)
101
102
102
103
### See also:
103
104
0 commit comments