by carrigan · 9y ago · 0 comments
In addition to the HAL annoyances that turbinerneiter talked about, I wound up giving up this time because I could find no way to manipulate strings in a static buffer. In C I could just allocate two 17 byte strings statically and then `sprintf` to them, but there seems to be no equivalent in Rust.
I'd be curious to know what limitations others have run into when trying this and if I was just doing it wrong.
use std::io::Write;
static mut S1: [u8; 17] = [0; 17];
fn main() {
unsafe {
write!(&mut S1[..], "Hi: {}", 5).unwrap();
let s = std::str::from_utf8_unchecked(&S1[..5]);
println!("string: {}", s);
}
}This is unsafe specifically because of the mutable static; you can deal with that in a few different ways, but that wasn't the point of the example.