From 438c4e1fd5051b9c91b99f4f1f183b2a1a2e67f9 Mon Sep 17 00:00:00 2001 From: grasshopper47 Date: Sat, 25 Nov 2023 14:56:53 +0100 Subject: [PATCH] Mention formatted strings --- .../data_types/03_strings.md | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/docs/versioned_docs/version-v0.17.0/language_concepts/data_types/03_strings.md b/docs/versioned_docs/version-v0.17.0/language_concepts/data_types/03_strings.md index c42f34ec3ad..e4c744babb6 100644 --- a/docs/versioned_docs/version-v0.17.0/language_concepts/data_types/03_strings.md +++ b/docs/versioned_docs/version-v0.17.0/language_concepts/data_types/03_strings.md @@ -15,8 +15,7 @@ keywords: The string type is a fixed length value defined with `str`. -You can use strings in `assert()` functions or print them with -`std::println()`. See more about [Logging](../../standard_library/logging). +You can use strings in `assert()` functions or print them with `std::println()`. See more about [Logging](../../standard_library/logging). ```rust use dep::std; @@ -42,7 +41,7 @@ fn main() { } ``` -## Escape characters +## Escape Characters You can use escape characters for your strings: @@ -58,6 +57,26 @@ You can use escape characters for your strings: Example: ```rust -let s = "Hello \"world" // prints "Hello "world" +let s = "Hello \"world"; // prints "Hello "world" let s = "hey \tyou"; // prints "hey you" ``` + +## Formatted Strings + +You can prepend a string with the singular `f` token to create a formatted string. This is useful when logging, as it allows injection of local variables: + +```rust + +let var = 15; + +std::println(f"var {var}") // prints "var 0x0F" + +// prints "Hello +//world" +std::println(f"Hello +world"); + +std::println(f"hey \tyou"); // prints "hey \tyou" +``` + +Note that escaped characters in formatted strings `fmtstr` will be outputted as defined, i.e. "\n" will be printed `\n`, not as a new line. You can add a newline or other whitespace by creating a multiline string as in the example above.