pub fn take<C, Input, Error: ParseError<Input>>(
count: C,
) -> impl Fn(Input) -> IResult<Input, Input, Error>
Expand description
Returns an input slice containing the first N input elements (Input[..N]).
It will return Err(Err::Error((_, ErrorKind::Eof)))
if the input is shorter than the argument.
ยงExample
use nom::bytes::complete::take;
fn take6(s: &str) -> IResult<&str, &str> {
take(6usize)(s)
}
assert_eq!(take6("1234567"), Ok(("7", "123456")));
assert_eq!(take6("things"), Ok(("", "things")));
assert_eq!(take6("short"), Err(Err::Error(Error::new("short", ErrorKind::Eof))));
assert_eq!(take6(""), Err(Err::Error(Error::new("", ErrorKind::Eof))));
The units that are taken will depend on the input type. For example, for a
&str
it will take a number of char
โs, whereas for a &[u8]
it will
take that many u8
โs:
use nom::error::Error;
use nom::bytes::complete::take;
assert_eq!(take::<_, _, Error<_>>(1usize)("๐"), Ok(("", "๐")));
assert_eq!(take::<_, _, Error<_>>(1usize)("๐".as_bytes()), Ok((b"\x9F\x92\x99".as_ref(), b"\xF0".as_ref())));