diff --git a/solutions/formatted-output/println.md b/solutions/formatted-output/println.md new file mode 100644 index 0000000..e0172f8 --- /dev/null +++ b/solutions/formatted-output/println.md @@ -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!"); +} +``` \ No newline at end of file diff --git a/src/SUMMARY.md b/src/SUMMARY.md index ad848fe..124d8c4 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/formatted-output/intro.md b/src/formatted-output/intro.md index c8b71b8..4810500 100644 --- a/src/formatted-output/intro.md +++ b/src/formatted-output/intro.md @@ -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 diff --git a/src/formatted-output/println.md b/src/formatted-output/println.md index 793c38d..515b687 100644 --- a/src/formatted-output/println.md +++ b/src/formatted-output/println.md @@ -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!"); +} +```