3 Printing is handled by a series of [`macros`][macros] defined in [`std::fmt`][fmt]
6 * `format!`: write formatted text to [`String`][string]
7 * `print!`: same as `format!` but the text is printed to the console (io::stdout).
8 * `println!`: same as `print!` but a newline is appended.
9 * `eprint!`: same as `format!` but the text is printed to the standard error (io::stderr).
10 * `eprintln!`: sames as `eprint!`but a newline is appended.
12 All parse text in the same fashion. A plus is that the formatting correctness will
13 be checked at compile time.
15 ```rust,editable,ignore,mdbook-runnable
17 // In general, the `{}` will be automatically replaced with any
18 // arguments. These will be stringified.
19 println!("{} days", 31);
21 // Without a suffix, 31 becomes an i32. You can change what type 31 is,
24 // There are various optional patterns this works with. Positional
25 // arguments can be used.
26 println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
28 // As can named arguments.
29 println!("{subject} {verb} {object}",
30 object="the lazy dog",
31 subject="the quick brown fox",
34 // Special formatting can be specified after a `:`.
35 println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
37 // You can right-align text with a specified width. This will output
38 // " 1". 5 white spaces and a "1".
39 println!("{number:>width$}", number=1, width=6);
41 // You can pad numbers with extra zeroes. This will output "000001".
42 println!("{number:>0width$}", number=1, width=6);
44 // It will even check to make sure the correct number of arguments are
46 println!("My name is {0}, {1} {0}", "Bond");
47 // FIXME ^ Add the missing argument: "James"
49 // Create a structure which contains an `i32`. Name it `Structure`.
51 struct Structure(i32);
53 // However, custom types such as this structure require more complicated
54 // handling. This will not work.
55 println!("This struct `{}` won't print...", Structure(3));
56 // FIXME ^ Comment out this line.
60 [`std::fmt`][fmt] contains many [`traits`][traits] which govern the display
61 of text. The base form of two important ones are listed below:
63 * `fmt::Debug`: Uses the `{:?}` marker. Format text for debugging purposes.
64 * `fmt::Display`: Uses the `{}` marker. Format text in a more elegant, user
67 Here, `fmt::Display` was used because the std library provides implementations
68 for these types. To print text for custom types, more steps are required.
72 * Fix the two issues in the above code (see FIXME) so that it runs without
74 * Add a `println!` macro that prints: `Pi is roughly 3.142` by controlling
75 the number of decimal places shown. For the purposes of this exercise,
76 use `let pi = 3.141592` as an estimate for Pi. (Hint: you may need to
77 check the [`std::fmt`][fmt] documentation for setting the number of
82 [`std::fmt`][fmt], [`macros`][macros], [`struct`][structs],
83 and [`traits`][traits]
85 [fmt]: https://doc.rust-lang.org/std/fmt/
87 [string]: std/str.html
88 [structs]: custom_types/structs.html