add example of preceded
@@ -7,7 +7,7 @@ use internal::IResult;
use error::ParseError;
/// Gets an object from the first parser,
-/// then get another object from the second parser.
+/// then gets another object from the second parser.
/// # Arguments
/// * `first` The first parser to apply.
/// * `second` The second parser to apply.
@@ -55,6 +55,34 @@ where
pair(first, second)(input)
}
+/// Matches an object from the first parser and discard it,
+/// then gets an object from the second parser.
+/// # Arguments
+/// * `first` The opening parser.
+/// * `second` The second parser to get object.
+/// ```rust
+/// # #[macro_use] extern crate nom;
+/// # use nom::{Err, error::ErrorKind, Needed};
+/// # use nom::Needed::Size;
+/// use nom::sequence::preceded;
+/// use nom::bytes::complete::tag;
+/// # fn main() {
+/// let first_parser = |s: &'static str| {
+/// tag::<_, _, (_, ErrorKind)>("abc")(s)
+/// };
+/// let second_parser = |s: &'static str| {
+/// tag::<_, _, (_, ErrorKind)>("efg")(s)
+/// };
+/// let parser = |s: &'static str| {
+/// preceded::<_, _, _, (_, ErrorKind), _, _>(first_parser, second_parser)(s)
+/// };
+///
+/// assert_eq!(parser("abcefg"), Ok(("", "efg")));
+/// assert_eq!(parser("abcefghij"), Ok(("hij", "efg")));
+/// assert_eq!(parser(""), Err(Err::Error(("", ErrorKind::Tag))));
+/// assert_eq!(parser("123"), Err(Err::Error(("123", ErrorKind::Tag))));
+/// # }
+/// ```
pub fn preceded<I, O1, O2, E: ParseError<I>, F, G>(first: F, second: G) -> impl Fn(I) -> IResult<I, O2, E>
where
F: Fn(I) -> IResult<I, O1, E>,
