rust-by-practice/en/src/formatted-output/println.md

41 lines
1.1 KiB
Markdown
Raw Normal View History

2022-03-15 03:24:29 -06:00
# println! and format!
2022-03-15 06:05:03 -06:00
Printing is handled by a series of [`macros`][macros] defined in [`std::fmt`][fmt]
2022-09-21 13:17:36 -05:00
Some of which include:
2022-03-15 06:05:03 -06:00
* `format!`: write formatted text to [`String`][string]
* `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 `format!` but the text is printed to the standard error (io::stderr).
* `eprintln!`: same as `eprint!`but a newline is appended.
2022-09-21 13:17:36 -05:00
All parse text in the same fashion. As a plus, Rust checks format correctness at compile time.
2022-03-15 06:05:03 -06:00
## `format!`
1.🌟
```rust,editable
fn main() {
let s1 = "hello";
/* Fill in the blank */
let s = format!(__);
assert_eq!(s, "hello, world!");
}
```
## `print!`, `println!`
2.🌟
```rust,editable
fn main() {
/* Fill in the blanks to make it print:
Hello world, I am
Sunface!
*/
__("hello world, ");
__("I am");
__("Sunface!");
}
```
2023-01-20 06:01:54 -06:00
> You can find the solutions [here](https://github.com/sunface/rust-by-practice/blob/master/solutions/formatted-output/println.md)(under the solutions path), but only use it when you need it :)