Skip to content

Commit

Permalink
docs(tutorial): Clarify examples in chapter 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Nukesor committed Nov 23, 2024
1 parent a13c1d4 commit 1ae59c3
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/_tutorial/chapter_5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@
//! # }
//! ```
//!
//! You'll notice that the above allows trailing `,` when we intended to not support that. We can
//! easily fix this by using [`separated`]:
//! You'll notice that the above allows trailing `,`. However, if that's not desired, it
//! can easily be fixed by using [`separated`] instead of [`repeat`]:
//! ```rust
//! # use winnow::prelude::*;
//! # use winnow::token::take_while;
Expand Down Expand Up @@ -199,9 +199,22 @@
//! }
//! ```
//!
//! If you look closely at [`repeat`], it isn't collecting directly into a [`Vec`] but
//! anything that implements the [`Accumulate`] trait to gather the results. This lets us make more complex parsers than we did in
//! [`chapter_2`] by accumulating the results into a `()` and [`take`][Parser::take]-ing the captured input:
//! If you look closely at [`separated`] and [`repeat`], they aren't limited to collecting
//! the result into a [`Vec`], but rather anything that implements the [`Accumulate`] trait.
//! [`Accumulate`] is for instance also implemented for [`HashSet`], [`String`] and `()`.
//!
//! This lets us build more complex parsers than we did in
//! [`chapter_2`] by accumulating the results into a `()` and [`take`][Parser::take]-ing
//! the consumed input.
//!
//! `take` works by
//! 1. Creating a [`checkpoint`][Stream::checkpoint]
//! 2. Running the inner parser, in our case the `parse_list` parser, which will advance the input
//! 3. Returning the slice from the first checkpoint to the current position.
//!
//! Since the result of `parse_list` gets thrown away, we
//! accumulates into a `()` to not waste work creating an unused `Vec`.
//!
//! ```rust
//! # use winnow::prelude::*;
//! # use winnow::token::take_while;
Expand Down Expand Up @@ -274,7 +287,9 @@ use crate::combinator;
use crate::combinator::repeat;
use crate::combinator::separated;
use crate::stream::Accumulate;
use crate::stream::Stream;
use crate::Parser;
use std::collections::HashSet;
use std::vec::Vec;

pub use super::chapter_4 as previous;
Expand Down

0 comments on commit 1ae59c3

Please sign in to comment.