Skip to main content

sections

Function sections 

Source
pub fn sections<'a>(
    items: &'a [Item],
) -> impl Iterator<Item = (Option<&'a Item>, &'a [Item])> + 'a
Available on crate feature markdown only.
Expand description

Groups the given Markdown Items by Item::Heading.

The returned iterator yields a (Option<&Item>, &[Item]) pair for each group, without cloning any Item:

  • The first element is the heading that starts the group, if any. It is None for the group of items that appears before the first heading, if there is any;
  • The second element is the slice of items that follow the heading, up to (but not including) the next one.

Every item in the given slice is yielded exactly once: a heading is returned as the first element of the group it starts, and every other item is part of the slice that follows the last heading before it.

ยงExample

use iced_widget::markdown;

let items: Vec<_> = markdown::parse("# Title\n\nHello!\n\n# Subtitle\n\nMore!").collect();

let mut groups = markdown::sections(&items);

let (heading, contents) = groups.next().unwrap();
assert!(heading.is_some());
assert_eq!(contents.len(), 1);

let (heading, contents) = groups.next().unwrap();
assert!(heading.is_some());
assert_eq!(contents.len(), 1);

assert!(groups.next().is_none());