@@ -45,6 +45,40 @@ write(
45
45
assert_eq! (buf , format! (" {:+05}" , 12 ));
46
46
```
47
47
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
+
48
82
# Features
49
83
By default only ` Display ` is supported, the rest of the
50
84
[ formatting traits] ( https://doc.rust-lang.org/std/fmt/index.html#formatting-traits )
@@ -53,3 +87,4 @@ can be enabled through the following features.
53
87
- ` debug ` enables ` ? ` , ` x? ` and ` X? ` trait specifiers
54
88
- ` number ` enables ` x ` , ` X ` , ` b ` , ` o ` , ` e ` and ` E ` trait specifiers
55
89
- ` pointer ` enables ` p ` trait specifiers
90
+ - ` iter ` enables ` i ` trait specifier
0 commit comments