diff --git a/src/tutorial/output.md b/src/tutorial/output.md index 60164ab1..b7e1edda 100644 --- a/src/tutorial/output.md +++ b/src/tutorial/output.md @@ -1,5 +1,110 @@ # Output +## Printing "Hello World" + +```rust +println!("Hello World"); +``` + +Well, that was easy. +Great, onto the next topic. + +## Using println + +You can pretty much print all the things you like +with the `println!` macro. +This macro has some pretty amazing capabilities, +but also a special syntax. +It expects you to write, +as a first parameter, +a literal string that with placeholders, +followed by the data you want to put into these placeholders +as further arguments. + +For example: + +```rust +let x = 42; +println!("My lucky number is {}.", x); +``` + +will print + +```console +My lucky number is 42. +``` + +The curly braces (`{}`) in the string above is one of these placeholders. +This is the default placeholder type +that tries to print the given in a human readable way. +For numbers and strings this works very well, +but not all types can do that. +This is why there is also a "debug representation", +that you can get by filling the braces of the placeholder like this: `{:?}`. + +For example, + +```rust +let xs = vec![1, 2, 3]; +println!("The list is: {:?}", xs); +``` + +will print + +```console +The list is: [1, 2, 3] +``` + +If you want your own data types to be printable for debugging and logging, +you can in most cases add a `#[derive(Debug)]` above their definition. + + + +## Printing errors + + + +## Showing a progress bar + +Some CLI applications run less than a second, +others take minutes or hours. +If you are writing one of the latter types of programs, +you might want to show the user that something is happening. +For this, you should try to printing useful status updates, +ideally in a form that can be easily consumed. + +Using the [indicatif] crate, +you can add progress bars +and little spinners to your program. + + + +[indicatif]: https://crates.io/crates/indicatif + ## Logging To make it easier to understand what is happening in our program, @@ -7,9 +112,29 @@ we might want to add some log statements. This is usually easy while writing your application. But it will become super helpful when running this program again in half a year. + + + +