Function lexical::parse_partial[][src]

pub fn parse_partial<N: FromLexical, Bytes: AsRef<[u8]>>(
    bytes: Bytes
) -> Result<(N, usize)>

High-level, partial conversion of decimal-encoded bytes to a number.

This functions parses as many digits as possible, returning the parsed value and the number of digits processed if at least one character is processed. If another error, such as numerical overflow or underflow occurs, this function returns the error code and the index at which the error occurred.

  • bytes - Byte slice to convert to number.

Examples


// String overloads
assert_eq!(lexical::parse_partial::<i32, _>("5"), Ok((5, 1)));
assert_eq!(lexical::parse_partial::<i32, _>("1a"), Ok((1, 1)));
assert_eq!(lexical::parse_partial::<f32, _>("0"), Ok((0.0, 1)));
assert_eq!(lexical::parse_partial::<f32, _>("1.0"), Ok((1.0, 3)));
assert_eq!(lexical::parse_partial::<f32, _>("1."), Ok((1.0, 2)));

// Bytes overloads
assert_eq!(lexical::parse_partial::<i32, _>(b"5"), Ok((5, 1)));
assert_eq!(lexical::parse_partial::<i32, _>(b"1a"), Ok((1, 1)));
assert_eq!(lexical::parse_partial::<f32, _>(b"0"), Ok((0.0, 1)));
assert_eq!(lexical::parse_partial::<f32, _>(b"1.0"), Ok((1.0, 3)));
assert_eq!(lexical::parse_partial::<f32, _>(b"1."), Ok((1.0, 2)));