I’d like to convert a usize
value into the corresponding chars ascii bytes.
For example, for the value 50usize
, I’d want to obtain [53, 48]
.
fn usize_to_ascii_bytes(input: usize) -> Vec<u8> {
...
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn usize_to_ascii_bytes_test() {
assert_eq!(vec![53], usize_to_ascii_bytes(5));
assert_eq!(vec![53, 48], usize_to_ascii_bytes(50));
}
}
Context: I’m doing the Redis CodeCrafters course, and for a GET request, I need to write the length of the value to the output stream; value being a vec of u8. I know I could convert that value’s .len()
to a string then obtain u8 in ascii, but I’m looking for the most effective way of doing this, minimizing conversions and copies, in the most idiomatic way in Rust.