add chapter [println and format]

This commit is contained in:
sunface 2022-03-15 20:05:03 +08:00
parent affa25fc54
commit 112f6b26ac
4 changed files with 57 additions and 13 deletions

View File

@ -0,0 +1,18 @@
1.
```rust
fn main() {
let s1 = "hello";
/* Fill in the blank */
let s = format!("{}, world!", s1);
assert_eq!(s, "hello, world!");
}
```
2.
```rust
fn main() {
print!("hello world, ");
println!("I am");
println!("Sunface!");
}
```

View File

@ -45,8 +45,8 @@
- [Advanced use and pub](crate-module/use-pub.md)
- [Comments and Docs](comments-docs.md)
- [Formatted output](formatted-output/intro.md)
- [Debug and Display](formatted-output/debug-display.md)
- [println! and format!](formatted-output/println.md)
- [Debug and Display](formatted-output/debug-display.md)
- [formating](formatted-output/formatting.md)
- [Lifetime TODO](lifetime/intro.md)
- [basic](lifetime/basic.md)

View File

@ -1,17 +1,5 @@
# Formatted output
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).
* `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.
All parse text in the same fashion. As a plus, Rust checks formatting
correctness at compile time.
```rust,editable,ignore,mdbook-runnable
fn main() {
// In general, the `{}` will be automatically replaced with any

View File

@ -1 +1,39 @@
# println! and format!
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).
* `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.
All parse text in the same fashion. As a plus, Rust checks formatting
correctness at compile time.
## `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!");
}
```