Skip to content

Commit ed2030f

Browse files
committed
add iter to README
1 parent 55f49d4 commit ed2030f

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,40 @@ write(
4545
assert_eq!(buf, format!("{:+05}", 12));
4646
```
4747

48+
# `i` iter format
49+
50+
The feature `iter` enables an additional format trait `i`, it allows to
51+
format a list of values with a format string and an optional join
52+
expression.
53+
54+
The syntax is `{list:i(the format string, '{it}' is the array element)(the
55+
optional join)}`, an empty join can also be omitted `{list:i({it})}`. Should
56+
you need to use `)` inside your format string or join, you can add `#`
57+
similar to rust's [raw string](https://doc.rust-lang.org/reference/tokens.html#raw-string-literals).
58+
59+
It is also possible to only iterate a sub-slice specified through a range
60+
before the format string, i.e. `{list:i1..4({it})}`. For open ranges range
61+
bounds can also be omitted. To index from the end, you can use negative
62+
range bounds.
63+
64+
A `Formattable` implementing iter is created using `Formattable::iter`:
65+
66+
```rs
67+
// HashMap macro
68+
use collection_literals::hash;
69+
use interpolator::{format, Formattable};
70+
// Needs to be a slice of references so because `Formattable::display` expects a
71+
// reference
72+
let items = [&"hello", &"hi", &"hey"].map(Formattable::display);
73+
let items = Formattable::iter(&items);
74+
let format_str = "Greetings: {items:i..-1(`{it}`)(, )} and {items:i-1..(`{it}`)}";
75+
assert_eq!(
76+
format(format_str, &hash!("items" => items))?,
77+
"Greetings: `hello`, `hi` and `hey`"
78+
);
79+
# return Ok::<(), interpolator::Error>(())
80+
```
81+
4882
# Features
4983
By default only `Display` is supported, the rest of the
5084
[formatting traits](https://doc.rust-lang.org/std/fmt/index.html#formatting-traits)
@@ -53,3 +87,4 @@ can be enabled through the following features.
5387
- `debug` enables `?`, `x?` and `X?` trait specifiers
5488
- `number` enables `x`, `X`, `b`, `o`, `e` and `E` trait specifiers
5589
- `pointer` enables `p` trait specifiers
90+
- `iter` enables `i` trait specifier

src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ format a list of values with a format string and an optional join
3030
expression.
3131
3232
The syntax is `{list:i(the format string, '{it}' is the array element)(the
33-
optional join)}`, an empty join can also be omitted `{list:i({it})}`. Should
34-
you need to use `)` inside your format string or join, you can add `#`
35-
similar to rust's [raw string](https://doc.rust-lang.org/reference/tokens.html#raw-string-literals).
33+
optional join)}`, an empty join can also be omitted `{list:i({it})}`.
34+
35+
Should you need to use `)` inside your format string or join, you can add `#`
36+
similar to rust's [raw string](https://doc.rust-lang.org/reference/tokens.html#raw-string-literals)
37+
(i.e. `#(({it}))#`).
3638
3739
It is also possible to only iterate a sub-slice specified through a range
3840
before the format string, i.e. `{list:i1..4({it})}`. For open ranges range
@@ -68,6 +70,7 @@ assert_eq!(
6870
//! - `debug` enables `?`, `x?` and `X?` trait specifiers
6971
//! - `number` enables `x`, `X`, `b`, `o`, `e` and `E` trait specifiers
7072
//! - `pointer` enables `p` trait specifiers
73+
//! - `iter` enables [`i`](#i-iter-format) trait specifier
7174
#![warn(clippy::pedantic, missing_docs)]
7275
#![allow(
7376
clippy::wildcard_imports,

0 commit comments

Comments
 (0)