]> git.proxmox.com Git - rustc.git/blame - src/doc/rust-by-example/src/hello/print.md
New upstream version 1.44.1+dfsg1
[rustc.git] / src / doc / rust-by-example / src / hello / print.md
CommitLineData
2c00a5a8
XL
1# Formatted print
2
3Printing is handled by a series of [`macros`][macros] defined in [`std::fmt`][fmt]
4some of which include:
5
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).
b7449926 10* `eprintln!`: same as `eprint!`but a newline is appended.
2c00a5a8 11
ba9703b0 12All parse text in the same fashion. As a plus, Rust checks formatting
dc9dc135 13correctness at compile time.
2c00a5a8
XL
14
15```rust,editable,ignore,mdbook-runnable
16fn main() {
17 // In general, the `{}` will be automatically replaced with any
18 // arguments. These will be stringified.
19 println!("{} days", 31);
20
dc9dc135 21 // Without a suffix, 31 becomes an i32. You can change what type 31 is
ba9703b0 22 // by providing a suffix. The number 31i64 for example has the type i64.
2c00a5a8
XL
23
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");
27
28 // As can named arguments.
29 println!("{subject} {verb} {object}",
30 object="the lazy dog",
31 subject="the quick brown fox",
32 verb="jumps over");
33
34 // Special formatting can be specified after a `:`.
35 println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
36
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);
40
41 // You can pad numbers with extra zeroes. This will output "000001".
42 println!("{number:>0width$}", number=1, width=6);
43
dc9dc135 44 // Rust even checks to make sure the correct number of arguments are
2c00a5a8
XL
45 // used.
46 println!("My name is {0}, {1} {0}", "Bond");
47 // FIXME ^ Add the missing argument: "James"
532ac7d7 48
dc9dc135 49 // Create a structure named `Structure` which contains an `i32`.
2c00a5a8
XL
50 #[allow(dead_code)]
51 struct Structure(i32);
52
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.
57}
58```
59
60[`std::fmt`][fmt] contains many [`traits`][traits] which govern the display
61of text. The base form of two important ones are listed below:
62
63* `fmt::Debug`: Uses the `{:?}` marker. Format text for debugging purposes.
64* `fmt::Display`: Uses the `{}` marker. Format text in a more elegant, user
65friendly fashion.
66
dc9dc135 67Here, we used `fmt::Display `because the std library provides implementations
2c00a5a8
XL
68for these types. To print text for custom types, more steps are required.
69
416331ca 70Implementing the `fmt::Display` trait automatically implements the
532ac7d7
XL
71[`ToString`] trait which allows us to [convert] the type to [`String`][string].
72
2c00a5a8
XL
73### Activities
74
75 * Fix the two issues in the above code (see FIXME) so that it runs without
76 error.
77 * Add a `println!` macro that prints: `Pi is roughly 3.142` by controlling
78 the number of decimal places shown. For the purposes of this exercise,
dc9dc135 79 use `let pi = 3.141592` as an estimate for pi. (Hint: you may need to
2c00a5a8
XL
80 check the [`std::fmt`][fmt] documentation for setting the number of
81 decimals to display)
82
e1599b0c 83### See also:
2c00a5a8
XL
84
85[`std::fmt`][fmt], [`macros`][macros], [`struct`][structs],
86and [`traits`][traits]
87
88[fmt]: https://doc.rust-lang.org/std/fmt/
dc9dc135
XL
89[macros]: ../macros.md
90[string]: ../std/str.md
91[structs]: ../custom_types/structs.md
92[traits]: ../trait.md
532ac7d7 93[`ToString`]: https://doc.rust-lang.org/std/string/trait.ToString.html
dc9dc135 94[convert]: ../conversion/string.md