Macro laby::iter

source ·
macro_rules! iter {
    ($expr:expr) => { ... };
    ($del:expr, $expr:expr) => { ... };
}
Expand description

Wraps an Iterator in RenderIterator, making it implement Render.

This is a convenience macro that wraps the given expression in RenderIterator or RenderIteratorDelimited, depending on the number of arguments.

When one argument is given, the expression is wrapped in RenderIterator which does not insert any delimiter between the items.

When two arguments are given, the first argument specifies the string delimiter to insert between items, and the second argument specifies the iterator expression. The arguments are wrapped in RenderIteratorDelimited.

If you are rendering an iterator with the newline "\n" string as the delimiter, consider using the iter_lines! macro instead.

Expansion

// iter!($expr)
{
    RenderIterator($expr.into_iter())
}

// iter!($del, $expr)
{
    RenderIteratorDelimited($expr.into_iter(), $del)
}

Example

Anything that implements Iterator<Item = T> where T implements Render can be rendered.

The following example renders three consecutive span elements, by mapping a range of numbers to a function that returns a node created by the span! macro. No delimiters are specified.

let s = iter!(
    (1..=3).into_iter().map(|i: u32| span!(i))
);

assert_eq!(render!(s), "<span>1</span><span>2</span><span>3</span>");

Another example renders three consecutive span elements, this time with the string ", " as the delimiter.

let s = iter!(
    ", ",
    (1..=3).into_iter().map(|i: u32| span!(i))
);

assert_eq!(render!(s), "<span>1</span>, <span>2</span>, <span>3</span>");

Note that whitespace in HTML can have different meanings depending on the context.