I want to do something like:
let x = 123;
let mut buf = [0 as u8; 20];
format_to!(x --> buf);
assert_eq!(&buf[..3], &b"123"[..]);
With #![no_std]
and without any memory allocator.
As I understand, there is an implementation of core::fmt::Display
for u64
, and I want to use it if possible.
In other words, I want to do something like format!(...)
, but without a memory allocator. How can I do this?
Let's start with the standard version:
If we then remove the standard library:
We get the error
write_fmt
is implemented in the core library bycore::fmt::Write
. If we implement it ourselves, we are able to pass that error:Note that we are duplicating the behavior of
io::Cursor
into this wrapper. Normally, multiple writes to a&mut [u8]
will overwrite each other. This is good for reusing allocation, but not useful when you have consecutive writes of the same data.Then it's just a matter of writing a macro if you want to.