Function lexical::parse_lossy [−][src]
pub fn parse_lossy<N: FromLexicalLossy, Bytes: AsRef<[u8]>>(
bytes: Bytes
) -> Result<N>
High-level lossy conversion of decimal-encoded bytes to a number.
This function uses aggressive optimizations to avoid worst-case
scenarios, and can return inaccurate results. For guaranteed accurate
floats, use parse
.
bytes
- Byte slice to convert to number.
Examples
// Get our error code. fn err_code<T>(r: lexical::Result<T>) -> ErrorCode { r.err().unwrap().code } // String overloads assert_eq!(lexical::parse_lossy::<f32, _>("0"), Ok(0.0)); assert_eq!(lexical::parse_lossy::<f32, _>("1.0"), Ok(1.0)); assert_eq!(lexical::parse_lossy::<f32, _>("1."), Ok(1.0)); assert_eq!(err_code(lexical::parse_lossy::<f32, _>("1a")), ErrorCode::InvalidDigit); // Bytes overloads assert_eq!(lexical::parse_lossy::<f32, _>(b"0"), Ok(0.0)); assert_eq!(lexical::parse_lossy::<f32, _>(b"1.0"), Ok(1.0)); assert_eq!(lexical::parse_lossy::<f32, _>(b"1."), Ok(1.0)); assert_eq!(err_code(lexical::parse_lossy::<f32, _>(b"1a")), ErrorCode::InvalidDigit);