Function winnow::combinator::success

source ·
pub fn success<I: Stream, O: Clone, E: ParseError<I>>(
    val: O
) -> impl FnMut(I) -> IResult<I, O, E>
Expand description

Always succeeds with given value without consuming any input.

For example, it can be used as the last alternative in alt to specify the default case.

Note: This never advances the Stream

Example

use winnow::branch::alt;
use winnow::combinator::{success, value};

let mut parser = success::<_,_,Error<_>>(10);
assert_eq!(parser("xyz"), Ok(("xyz", 10)));

let mut sign = alt((value(-1, '-'), value(1, '+'), success::<_,_,Error<_>>(1)));
assert_eq!(sign("+10"), Ok(("10", 1)));
assert_eq!(sign("-10"), Ok(("10", -1)));
assert_eq!(sign("10"), Ok(("10", 1)));