Skip to content

Commit d4023bd

Browse files
authored
Merge pull request #1650 from adrian5/cleanup
Clean up Chapter 1 (Hello World)
2 parents a9869b4 + 81c7b52 commit d4023bd

File tree

7 files changed

+66
-66
lines changed

7 files changed

+66
-66
lines changed

src/hello.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
This is the source code of the traditional Hello World program.
44

55
```rust,editable
6-
// This is a comment, and is ignored by the compiler
6+
// This is a comment, and is ignored by the compiler.
77
// You can test this code by clicking the "Run" button over there ->
8-
// or if you prefer to use your keyboard, you can use the "Ctrl + Enter" shortcut
8+
// or if you prefer to use your keyboard, you can use the "Ctrl + Enter"
9+
// shortcut.
910
1011
// This code is editable, feel free to hack it!
1112
// You can always return to the original code by clicking the "Reset" button ->
1213
13-
// This is the main function
14+
// This is the main function.
1415
fn main() {
15-
// Statements here are executed when the compiled binary is called
16+
// Statements here are executed when the compiled binary is called.
1617
17-
// Print text to the console
18+
// Print text to the console.
1819
println!("Hello World!");
1920
}
2021
```
@@ -38,8 +39,7 @@ Hello World!
3839
### Activity
3940

4041
Click 'Run' above to see the expected output. Next, add a new
41-
line with a second `println!` macro so that the output
42-
shows:
42+
line with a second `println!` macro so that the output shows:
4343

4444
```text
4545
Hello World!

src/hello/comment.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,29 @@ Any program requires comments, and Rust supports
44
a few different varieties:
55

66
* *Regular comments* which are ignored by the compiler:
7-
* `// Line comments which go to the end of the line.`
8-
* `/* Block comments which go to the closing delimiter. */`
9-
* *Doc comments* which are parsed into HTML library
10-
[documentation][docs]:
11-
* `/// Generate library docs for the following item.`
12-
* `//! Generate library docs for the enclosing item.`
7+
* `// Line comments which go to the end of the line.`
8+
* `/* Block comments which go to the closing delimiter. */`
9+
* *Doc comments* which are parsed into HTML library [documentation][docs]:
10+
* `/// Generate library docs for the following item.`
11+
* `//! Generate library docs for the enclosing item.`
1312

1413
```rust,editable
1514
fn main() {
16-
// This is an example of a line comment
17-
// There are two slashes at the beginning of the line
18-
// And nothing written inside these will be read by the compiler
15+
// This is an example of a line comment.
16+
// There are two slashes at the beginning of the line.
17+
// And nothing written inside these will be read by the compiler.
1918
2019
// println!("Hello, world!");
2120
2221
// Run it. See? Now try deleting the two slashes, and run it again.
2322
24-
/*
23+
/*
2524
* This is another type of comment, a block comment. In general,
26-
* line comments are the recommended comment style. But
27-
* block comments are extremely useful for temporarily disabling
28-
* chunks of code. /* Block comments can be /* nested, */ */
29-
* so it takes only a few keystrokes to comment out everything
30-
* in this main() function. /*/*/* Try it yourself! */*/*/
25+
* line comments are the recommended comment style. But block comments
26+
* are extremely useful for temporarily disabling chunks of code.
27+
* /* Block comments can be /* nested, */ */ so it takes only a few
28+
* keystrokes to comment out everything in this main() function.
29+
* /*/*/* Try it yourself! */*/*/
3130
*/
3231
3332
/*
@@ -41,7 +40,6 @@ fn main() {
4140
let x = 5 + /* 90 + */ 5;
4241
println!("Is `x` 10 or 100? x = {}", x);
4342
}
44-
4543
```
4644

4745
### See also:

src/hello/print.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Formatted print
22

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:
55

66
* `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).
89
* `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).
1012
* `eprintln!`: same as `eprint!` but a newline is appended.
1113

1214
All parse text in the same fashion. As a plus, Rust checks formatting
@@ -20,7 +22,7 @@ fn main() {
2022
2123
// Positional arguments can be used. Specifying an integer inside `{}`
2224
// 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.
2426
println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
2527
2628
// As can named arguments.
@@ -29,13 +31,13 @@ fn main() {
2931
subject="the quick brown fox",
3032
verb="jumps over");
3133
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
3941
4042
4143
// You can right-justify text with a specified width. This will
@@ -46,29 +48,28 @@ fn main() {
4648
//and left-adjust by flipping the sign. This will output "10000".
4749
println!("{number:0<5}", number=1);
4850
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 `$`.
5052
println!("{number:0>width$}", number=1, width=5);
5153
5254
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.
5556
println!("My name is {0}, {1} {0}", "Bond");
5657
// FIXME ^ Add the missing argument: "James"
5758
5859
// 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.
6061
6162
#[allow(dead_code)]
6263
struct Structure(i32);
6364
6465
// This will not compile because `Structure` does not implement
65-
// fmt::Display
66+
// fmt::Display.
6667
//println!("This struct `{}` won't print...", Structure(3));
6768
// TODO ^ Try uncommenting this line
6869
6970
// For Rust 1.58 and above, you can directly capture the argument from a
7071
// 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".
7273
let number: f64 = 1.0;
7374
let width: usize = 5;
7475
println!("{number:>width$}");
@@ -80,7 +81,7 @@ of text. The base form of two important ones are listed below:
8081

8182
* `fmt::Debug`: Uses the `{:?}` marker. Format text for debugging purposes.
8283
* `fmt::Display`: Uses the `{}` marker. Format text in a more elegant, user
83-
friendly fashion.
84+
friendly fashion.
8485

8586
Here, we used `fmt::Display` because the std library provides implementations
8687
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
9091

9192
### Activities
9293

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)
101102

102103
### See also:
103104

src/hello/print/fmt.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
We've seen that formatting is specified via a *format string*:
44

55
* `format!("{}", foo)` -> `"3735928559"`
6-
* `format!("0x{:X}", foo)` ->
7-
[`"0xDEADBEEF"`][deadbeef]
6+
* `format!("0x{:X}", foo)` -> [`"0xDEADBEEF"`][deadbeef]
87
* `format!("0o{:o}", foo)` -> `"0o33653337357"`
98

109
The same variable (`foo`) can be formatted differently depending on which
@@ -26,13 +25,13 @@ struct City {
2625
}
2726
2827
impl Display for City {
29-
// `f` is a buffer, and this method must write the formatted string into it
28+
// `f` is a buffer, and this method must write the formatted string into it.
3029
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
3130
let lat_c = if self.lat >= 0.0 { 'N' } else { 'S' };
3231
let lon_c = if self.lon >= 0.0 { 'E' } else { 'W' };
3332
3433
// `write!` is like `format!`, but it will write the formatted string
35-
// into a buffer (the first argument)
34+
// into a buffer (the first argument).
3635
write!(f, "{}: {:.3}°{} {:.3}°{}",
3736
self.name, self.lat.abs(), lat_c, self.lon.abs(), lon_c)
3837
}
@@ -69,6 +68,7 @@ You can view a [full list of formatting traits][fmt_traits] and their argument
6968
types in the [`std::fmt`][fmt] documentation.
7069

7170
### Activity
71+
7272
Add an implementation of the `fmt::Display` trait for the `Color` struct above
7373
so that the output displays as:
7474

@@ -79,8 +79,9 @@ RGB (0, 0, 0) 0x000000
7979
```
8080

8181
Two hints if you get stuck:
82-
* You [may need to list each color more than once][named_parameters],
83-
* You can [pad with zeros to a width of 2][fmt_width] with `:0>2`.
82+
83+
* You [may need to list each color more than once][named_parameters].
84+
* You can [pad with zeros to a width of 2][fmt_width] with `:0>2`.
8485

8586
### See also:
8687

src/hello/print/print_debug.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ fn main() {
4343
4444
// `Structure` is printable!
4545
println!("Now {:?} will print!", Structure(3));
46-
46+
4747
// The problem with `derive` is there is no control over how
4848
// the results look. What if I want this to just show a `7`?
4949
println!("Now {:?} will print!", Deep(Structure(7)));
5050
}
5151
```
5252

53-
So `fmt::Debug` definitely makes this printable but sacrifices some
54-
elegance. Rust also provides "pretty printing" with `{:#?}`.
53+
So `fmt::Debug` definitely makes this printable but sacrifices some elegance.
54+
Rust also provides "pretty printing" with `{:#?}`.
5555

5656
```rust,editable
5757
#[derive(Debug)]

src/hello/print/print_display.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ or for any other generic containers. `fmt::Debug` must then be used for these
4141
generic cases.
4242

4343
This is not a problem though because for any new *container* type which is
44-
*not* generic,`fmt::Display` can be implemented.
44+
*not* generic, `fmt::Display` can be implemented.
4545

4646
```rust,editable
4747
use std::fmt; // Import `fmt`
@@ -66,7 +66,7 @@ struct Point2D {
6666
y: f64,
6767
}
6868
69-
// Similarly, implement `Display` for `Point2D`
69+
// Similarly, implement `Display` for `Point2D`.
7070
impl fmt::Display for Point2D {
7171
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7272
// Customize so only `x` and `y` are denoted.
@@ -100,10 +100,9 @@ fn main() {
100100
}
101101
```
102102

103-
So, `fmt::Display` has been implemented but `fmt::Binary` has not, and
104-
therefore cannot be used. `std::fmt` has many such [`traits`][traits] and
105-
each requires its own implementation. This is detailed further in
106-
[`std::fmt`][fmt].
103+
So, `fmt::Display` has been implemented but `fmt::Binary` has not, and therefore
104+
cannot be used. `std::fmt` has many such [`traits`][traits] and each requires
105+
its own implementation. This is detailed further in [`std::fmt`][fmt].
107106

108107
### Activity
109108

src/hello/print/print_display/testcase_list.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ fn main() {
5252

5353
### Activity
5454

55-
Try changing the program so that the index of each element in the vector is also printed. The new output should look like this:
55+
Try changing the program so that the index of each element in the vector is also
56+
printed. The new output should look like this:
5657

5758
```rust,ignore
5859
[0: 1, 1: 2, 2: 3]

0 commit comments

Comments
 (0)