@@ -7,16 +7,16 @@ println!("Hello World");
77```
88
99Well, that was easy.
10- Great, onto the next topic.
10+ Great! Onto the next topic.
1111
1212## Using ` println! `
1313
1414You can pretty much print all the things you like
1515with the ` println! ` macro.
1616This macro has some pretty amazing capabilities,
1717but also a special syntax.
18- It expects you to write a string literal as the first parameter,
19- that contains placeholders that will be filled in
18+ It expects a string literal that contains placeholders
19+ as the first parameter. The string will be filled in
2020by the values of the parameters that follow as further arguments.
2121
2222For example:
@@ -26,7 +26,7 @@ let x = 42;
2626println! (" My lucky number is {}." , x );
2727```
2828
29- will print
29+ will print:
3030
3131``` console
3232My lucky number is 42.
@@ -35,31 +35,31 @@ My lucky number is 42.
3535The curly braces (` {} ` ) in the string above is one of these placeholders.
3636This is the default placeholder type
3737that tries to print the given value in a human readable way.
38- For numbers and strings this works very well,
38+ For numbers and strings, this works very well,
3939but not all types can do that.
40- This is why there is also a "debug representation",
40+ This is why there is also a "debug representation"
4141that you can get by filling the braces of the placeholder like this: ` {:?} ` .
4242
43- For example,
43+ For example:
4444
4545``` rust
4646let xs = vec! [1 , 2 , 3 ];
4747println! (" The list is: {:?}" , xs );
4848```
4949
50- will print
50+ will print:
5151
5252``` console
5353The list is: [1, 2, 3]
5454```
5555
5656If you want your own data types to be printable for debugging and logging,
57- you can in most cases add a ` #[derive(Debug)] ` above their definition.
57+ you can typically add a ` #[derive(Debug)] ` above their definition.
5858
5959<aside >
6060
6161** Note:**
62- "User-friendly" printing is done using the [ ` Display ` ] trait,
62+ "User-friendly" printing is done using the [ ` Display ` ] trait and
6363debug output (human-readable but targeted at developers) uses the [ ` Debug ` ] trait.
6464You can find more information about the syntax you can use in ` println! `
6565in the [ documentation for the ` std::fmt ` module] [ std::fmt ] .
@@ -82,16 +82,16 @@ or more tools.
8282
8383** Note:**
8484On most operating systems,
85- a program can write to two output streams, ` stdout ` and ` stderr ` .
86- ` stdout ` is for the program's actual output,
85+ a program can write to two output streams: ` stdout ` and ` stderr ` .
86+ ` stdout ` is for the program's actual output
8787while ` stderr ` allows errors and other messages to be kept separate from ` stdout ` .
8888That way,
8989output can be stored to a file or piped to another program
9090while errors are shown to the user.
9191
9292</aside >
9393
94- In Rust this is achieved
94+ In Rust, this is achieved
9595with ` println! ` and ` eprintln! ` ,
9696the former printing to ` stdout `
9797and the latter to ` stderr ` .
@@ -103,13 +103,13 @@ eprintln!("This is an error! :(");
103103
104104<aside >
105105
106- ** Beware** : Printing [ escape codes] can be dangerous,
107- putting the user's terminal into a weird state.
106+ ** Beware** : Printing [ escape codes] can be dangerous and
107+ put the user's terminal into a weird state.
108108Always be careful when manually printing them!
109109
110110[ escape codes ] : https://en.wikipedia.org/wiki/ANSI_escape_code
111111
112- Ideally you should be using a crate like ` ansi_term `
112+ Ideally, you should be using a crate like ` ansi_term `
113113when dealing with raw escape codes
114114to make your (and your user's) life easier.
115115
@@ -126,13 +126,13 @@ there are two things you can do.
126126First,
127127you might want to reduce the number of writes
128128that actually "flush" to the terminal.
129- ` println! ` tells the system to flush to the terminal _ every_ time,
129+ ` println! ` tells the system to flush to the terminal _ every_ time
130130because it is common to print each new line.
131131If you don't need that,
132- you can wrap your ` stdout ` handle in a [ ` BufWriter ` ]
133- which by default buffers up to 8 kB.
134- ( You can still call ` .flush() ` on this ` BufWriter `
135- when you want to print immediately.)
132+ you can wrap your ` stdout ` handle in a [ ` BufWriter ` ] ,
133+ which buffers up to 8 kB by default .
134+ You can still call ` .flush() ` on this ` BufWriter `
135+ when you want to print immediately.
136136
137137``` rust
138138use std :: io :: {self , Write };
@@ -161,7 +161,7 @@ You can also combine the two approaches.
161161
162162## Showing a progress bar
163163
164- Some CLI applications run less than a second,
164+ Some CLI applications run less than a second while
165165others take minutes or hours.
166166If you are writing one of the latter types of programs,
167167you might want to show the user that something is happening.
@@ -189,10 +189,10 @@ for more information.
189189
190190To make it easier to understand what is happening in our program,
191191we might want to add some log statements.
192- This is usually easy while writing your application.
193- But it will become super helpful when running this program again in half a year.
194- In some regard ,
195- logging is the same as using ` println! ` ,
192+ This is usually easy while writing your application,
193+ and it will become super helpful when running this program again in half a year.
194+ In some ways ,
195+ logging is the same as using ` println! `
196196except that you can specify the importance of a message.
197197The levels you can usually use are _ error_ , _ warn_ , _ info_ , _ debug_ , and _ trace_
198198(_ error_ has the highest priority, _ trace_ the lowest).
@@ -203,16 +203,16 @@ The [log] crate (this contains macros named after the log level)
203203and an _ adapter_ that actually writes the log output somewhere useful.
204204Having the ability to use log adapters is very flexible:
205205You can, for example, use them to write logs not only to the terminal
206- but also to [ syslog] , or to a central log server.
206+ but also to [ syslog] or to a central log server.
207207
208208[ syslog ] : https://en.wikipedia.org/wiki/Syslog
209209
210- Since we are right now only concerned with writing a CLI application,
210+ Since we are only concerned with writing a CLI application,
211211an easy adapter to use is [ env_logger] .
212212It's called "env" logger because you can
213213use an environment variable to specify which parts of your application
214214you want to log
215- ( and at which level you want to log them) .
215+ and at which level you want to log them.
216216It will prefix your log messages with a timestamp
217217and the module where the log messages come from.
218218Since libraries can also use ` log ` ,
@@ -260,20 +260,20 @@ $ cargo run --bin output-log
260260` RUST_LOG ` is the name of the environment variable
261261you can use to set your log settings.
262262` env_logger ` also contains a builder
263- so you can programmatically adjust these settings,
264- and, for example, also show _ info_ level messages by default.
263+ so you can programmatically adjust these settings
264+ like showing _ info_ level messages by default.
265265
266- There are a lot of alternative logging adapters out there,
267- and also alternatives or extensions to ` log ` .
266+ There are a lot of alternative logging adapters out there
267+ as well as alternatives and extensions to ` log ` .
268268If you know your application will have a lot to log,
269- make sure to review them,
270- and make your users' life easier.
269+ make sure to review them
270+ and make your users' lives easier.
271271
272272<aside >
273273
274274** Tip:**
275- Experience has shown that even mildly useful CLI programs can end up being used for years to come.
276- (Especially if they were meant as a temporary solution.)
275+ Experience has shown that even mildly useful CLI programs can end up being used for years to come,
276+ especially if they were meant as a temporary solution.
277277If your application doesn't work
278278and someone (e.g., you, in the future) needs to figure out why,
279279being able to pass ` --verbose ` to get additional log output
0 commit comments