mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 12:39:42 +00:00
add chapter [println and format]
This commit is contained in:
18
solutions/formatted-output/println.md
Normal file
18
solutions/formatted-output/println.md
Normal 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!");
|
||||||
|
}
|
||||||
|
```
|
@ -45,8 +45,8 @@
|
|||||||
- [Advanced use and pub](crate-module/use-pub.md)
|
- [Advanced use and pub](crate-module/use-pub.md)
|
||||||
- [Comments and Docs](comments-docs.md)
|
- [Comments and Docs](comments-docs.md)
|
||||||
- [Formatted output](formatted-output/intro.md)
|
- [Formatted output](formatted-output/intro.md)
|
||||||
- [Debug and Display](formatted-output/debug-display.md)
|
|
||||||
- [println! and format!](formatted-output/println.md)
|
- [println! and format!](formatted-output/println.md)
|
||||||
|
- [Debug and Display](formatted-output/debug-display.md)
|
||||||
- [formating](formatted-output/formatting.md)
|
- [formating](formatted-output/formatting.md)
|
||||||
- [Lifetime TODO](lifetime/intro.md)
|
- [Lifetime TODO](lifetime/intro.md)
|
||||||
- [basic](lifetime/basic.md)
|
- [basic](lifetime/basic.md)
|
||||||
|
@ -1,17 +1,5 @@
|
|||||||
# Formatted output
|
# 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
|
```rust,editable,ignore,mdbook-runnable
|
||||||
fn main() {
|
fn main() {
|
||||||
// In general, the `{}` will be automatically replaced with any
|
// In general, the `{}` will be automatically replaced with any
|
||||||
|
@ -1 +1,39 @@
|
|||||||
# println! and format!
|
# 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!");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Reference in New Issue
Block a user