summaryrefslogtreecommitdiffstats
path: root/src/doc/rust-by-example/src/hello/print.md
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/doc/rust-by-example/src/hello/print.md66
1 files changed, 35 insertions, 31 deletions
diff --git a/src/doc/rust-by-example/src/hello/print.md b/src/doc/rust-by-example/src/hello/print.md
index cce838fc2..55f6ed520 100644
--- a/src/doc/rust-by-example/src/hello/print.md
+++ b/src/doc/rust-by-example/src/hello/print.md
@@ -1,12 +1,14 @@
# Formatted print
-Printing is handled by a series of [`macros`][macros] defined in [`std::fmt`][fmt]
-some of which include:
+Printing is handled by a series of [`macros`][macros] defined in
+[`std::fmt`][fmt] some of which include:
* `format!`: write formatted text to [`String`][string]
-* `print!`: same as `format!` but the text is printed to the console (io::stdout).
+* `print!`: same as `format!` but the text is printed to the console
+ (io::stdout).
* `println!`: same as `print!` but a newline is appended.
-* `eprint!`: same as `print!` but the text is printed to the standard error (io::stderr).
+* `eprint!`: same as `print!` but the text is printed to the standard error
+ (io::stderr).
* `eprintln!`: same as `eprint!` but a newline is appended.
All parse text in the same fashion. As a plus, Rust checks formatting
@@ -20,7 +22,7 @@ fn main() {
// Positional arguments can be used. Specifying an integer inside `{}`
// determines which additional argument will be replaced. Arguments start
- // at 0 immediately after the format string
+ // at 0 immediately after the format string.
println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
// As can named arguments.
@@ -29,13 +31,13 @@ fn main() {
subject="the quick brown fox",
verb="jumps over");
- // Different formatting can be invoked by specifying the format character after a
- // `:`.
- println!("Base 10: {}", 69420); //69420
- println!("Base 2 (binary): {:b}", 69420); //10000111100101100
- println!("Base 8 (octal): {:o}", 69420); //207454
- println!("Base 16 (hexadecimal): {:x}", 69420); //10f2c
- println!("Base 16 (hexadecimal): {:X}", 69420); //10F2C
+ // Different formatting can be invoked by specifying the format character
+ // after a `:`.
+ println!("Base 10: {}", 69420); // 69420
+ println!("Base 2 (binary): {:b}", 69420); // 10000111100101100
+ println!("Base 8 (octal): {:o}", 69420); // 207454
+ println!("Base 16 (hexadecimal): {:x}", 69420); // 10f2c
+ println!("Base 16 (hexadecimal): {:X}", 69420); // 10F2C
// You can right-justify text with a specified width. This will
@@ -43,32 +45,31 @@ fn main() {
println!("{number:>5}", number=1);
// You can pad numbers with extra zeroes,
- //and left-adjust by flipping the sign. This will output "10000".
+ // and left-adjust by flipping the sign. This will output "10000".
println!("{number:0<5}", number=1);
- // You can use named arguments in the format specifier by appending a `$`
+ // You can use named arguments in the format specifier by appending a `$`.
println!("{number:0>width$}", number=1, width=5);
- // Rust even checks to make sure the correct number of arguments are
- // used.
+ // Rust even checks to make sure the correct number of arguments are used.
println!("My name is {0}, {1} {0}", "Bond");
// FIXME ^ Add the missing argument: "James"
// Only types that implement fmt::Display can be formatted with `{}`. User-
- // defined types do not implement fmt::Display by default
+ // defined types do not implement fmt::Display by default.
- #[allow(dead_code)]
+ #[allow(dead_code)] // disable `dead_code` which warn against unused module
struct Structure(i32);
// This will not compile because `Structure` does not implement
- // fmt::Display
+ // fmt::Display.
//println!("This struct `{}` won't print...", Structure(3));
// TODO ^ Try uncommenting this line
// For Rust 1.58 and above, you can directly capture the argument from a
// surrounding variable. Just like the above, this will output
- // " 1". 4 white spaces and a "1".
+ // " 1", 4 white spaces and a "1".
let number: f64 = 1.0;
let width: usize = 5;
println!("{number:>width$}");
@@ -80,7 +81,7 @@ of text. The base form of two important ones are listed below:
* `fmt::Debug`: Uses the `{:?}` marker. Format text for debugging purposes.
* `fmt::Display`: Uses the `{}` marker. Format text in a more elegant, user
-friendly fashion.
+ friendly fashion.
Here, we used `fmt::Display` because the std library provides implementations
for these types. To print text for custom types, more steps are required.
@@ -88,21 +89,22 @@ for these types. To print text for custom types, more steps are required.
Implementing the `fmt::Display` trait automatically implements the
[`ToString`] trait which allows us to [convert] the type to [`String`][string].
+In *line 46*, `#[allow(dead_code)]` is an [attribute] which only apply to the module after it.
+
### Activities
- * Fix the issue in the above code (see FIXME) so that it runs without
- error.
- * Try uncommenting the line that attempts to format the `Structure` struct (see TODO)
- * Add a `println!` macro call that prints: `Pi is roughly 3.142` by controlling
- the number of decimal places shown. For the purposes of this exercise,
- use `let pi = 3.141592` as an estimate for pi. (Hint: you may need to
- check the [`std::fmt`][fmt] documentation for setting the number of
- decimals to display)
+* Fix the issue in the above code (see FIXME) so that it runs without
+ error.
+* Try uncommenting the line that attempts to format the `Structure` struct
+ (see TODO)
+* Add a `println!` macro call that prints: `Pi is roughly 3.142` by controlling
+ the number of decimal places shown. For the purposes of this exercise, use
+ `let pi = 3.141592` as an estimate for pi. (Hint: you may need to check the
+ [`std::fmt`][fmt] documentation for setting the number of decimals to display)
### See also:
-[`std::fmt`][fmt], [`macros`][macros], [`struct`][structs],
-and [`traits`][traits]
+[`std::fmt`][fmt], [`macros`][macros], [`struct`][structs], [`traits`][traits], and [`dead_code`][dead_code]
[fmt]: https://doc.rust-lang.org/std/fmt/
[macros]: ../macros.md
@@ -111,3 +113,5 @@ and [`traits`][traits]
[traits]: https://doc.rust-lang.org/std/fmt/#formatting-traits
[`ToString`]: https://doc.rust-lang.org/std/string/trait.ToString.html
[convert]: ../conversion/string.md
+[attribute]: ../attribute.md
+[dead_code]: ../attribute/unused.md