Skip to content

Commit

Permalink
Mention formatted strings
Browse files Browse the repository at this point in the history
  • Loading branch information
grasshopper47 committed Nov 25, 2023
1 parent 9eb1d08 commit 438c4e1
Showing 1 changed file with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ keywords:

The string type is a fixed length value defined with `str<N>`.

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).

Check warning on line 18 in docs/versioned_docs/version-v0.17.0/language_concepts/data_types/03_strings.md

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (println)

```rust
use dep::std;
Expand All @@ -42,7 +41,7 @@ fn main() {
}
```

## Escape characters
## Escape Characters

You can use escape characters for your strings:

Expand All @@ -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"

Check warning on line 72 in docs/versioned_docs/version-v0.17.0/language_concepts/data_types/03_strings.md

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (println)

// prints "Hello
//world"
std::println(f"Hello

Check warning on line 76 in docs/versioned_docs/version-v0.17.0/language_concepts/data_types/03_strings.md

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (println)
world");

std::println(f"hey \tyou"); // prints "hey \tyou"

Check warning on line 79 in docs/versioned_docs/version-v0.17.0/language_concepts/data_types/03_strings.md

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (println)
```

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.

0 comments on commit 438c4e1

Please sign in to comment.