Function lexical_core::write
source · [−]pub fn write<N: ToLexical>(n: N, bytes: &mut [u8]) -> &mut [u8]ⓘNotable traits for &'_ mut [u8]impl<'_> Write for &'_ mut [u8]impl<'_> Read for &'_ [u8]
Expand description
Write number to string.
Returns a subslice of the input buffer containing the written bytes, starting from the same address in memory as the input slice.
value
- Number to serialize.bytes
- Buffer to write number to.
Panics
Panics if the buffer may not be large enough to hold the serialized
number. In order to ensure the function will not panic, provide a
buffer with at least {integer}::FORMATTED_SIZE
elements.
Example
#[cfg(feature = "write-floats")] {
// import `BUFFER_SIZE` to get the maximum bytes written by the number.
use lexical_core::BUFFER_SIZE;
let mut buffer = [0u8; BUFFER_SIZE];
let float = 3.14159265359_f32;
lexical_core::write(float, &mut buffer);
assert_eq!(&buffer[0..9], b"3.1415927");
This will panic, because the buffer is not large enough:
ⓘ
// note: the buffer is only one byte large
let mut buffer = [0u8; 1];
let float = 3.14159265359_f32;
lexical_core::write(float, &mut buffer);