Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The 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);
        }
    }


Having all your main inside unsafe doesn't seem like the best advert for Rust :)


You'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.


This 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 :)


No problem! It is slightly obtuse, given that you store a u8 buffer and then make &strs from it, rather than storing a &str.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: