ext_format
A small, yet powerful, Rust crate for string interpolation. Inspired by Rust's macro rules, it provides two main macros: ext_format! and ext_format_unindent!
The ext_format_unindent! macro works exactly like ext_format!, but first trims leading whitespace, to make working with multiline strings less painful.
Installation
Add the following to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Interpolation
Use $ for basic interpolation:
let name = "Alice";
let output = ext_format!;
Binding new variable names
Use {name:new_name} to bind a new name to a variable.
let number = 42;
let output = ext_format!;
// Output: "Number: 42 42 42"
Basic Repetition
$($var)*: No separators$($var),*: Character as a separator$($var)(...)*: String as a separator (supports escaped characters)
let numbers = vec!;
let output = ext_format!;
// Output: "Numbers: 1, 2, 3"
For using newlines as separators:
let items = vec!;
let output = ext_format!;
// Output:
// Items:
// apple
// banana
// cherry
Repetition with Hidden Variables
Use @ to include variables that control the loop but aren't included in the output.
let items = vec!;
let counter = vec!;
let output = ext_format!;
// Output:
// Items:
// apple
// banana
Repetition with named Iteration Variables
Use {name:new_name} to bind a Name to a Variable.
let numbers = vec!;
let output = ext_format!;
// Output: "Numbers: 1 1, 2 2, 3 3"
Nested Repetitions
Repetitions can contain other repetitions, acting like nested for-loops:
let matrix = vec!;
let output = ext_format!;
// Output:
// Matrix:
// 1 2 3
// 4 5 6
// 7 8 9
Zipped Variables
Variables in a single repetition layer are automatically zipped together, meaning they iterate in lockstep.
let names = vec!;
let ages = vec!;
let output = ext_format!;
// Profiles:
// Alice 30
// Bob 40
Multiline Strings
For multiline strings, ext_format_unindented can be used to avoid leading whitespace:
let output = unindented;
// Output:
// void func3() {
// printf("1 2 3");
// printf("4 5 6");
// printf("7 8 9");
// }
If the regular ext_format was used here, it would result in the following:
let output = indented;
// Output:
// void func3() {
// printf("1 2 3");
// printf("4 5 6");
// printf("7 8 9");
// }
With the indentation of the resulting string depending on the indentation of the function itself.
License
This project is licensed under the MIT License. See the LICENSE.md file for details.