4 ms·
I go through this cycle every 6 months or so where I will get excited about Rust and try it out for embedded development again. Typically in the first day, I ge
by carrigan 9y ago
I go through this cycle every 6 months or so where I will get excited about Rust and try it out for embedded development again. Typically in the first day, I get frustrated with some major limitation and put it back on the shelf. This last time, I wanted to make a simple timer that displayed the time since a button was last pressed on a 16x2 character LCD screen.
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.
- steveklabnik 9y agoThe write! macro would be the equivalent here. Something like this: 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); } }
- carrigan 9y agoThis is exactly what I was looking for; I had only checked the String and &str documentation and didn't think to look for a macro. Thanks :)
- steveklabnik 9y agoNo problem! It is slightly obtuse, given that you store a u8 buffer and then make &strs from it, rather than storing a &str.
- pjc50 9y agoHaving all your main inside unsafe doesn't seem like the best advert for Rust :)
- steveklabnik 9y agoYou're right, it's not; I was just showing the smallest code. :) 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.