News for hackers. Stuff that matters.
Re:What Is Rust's Unsafe? (2019) by mynameisash on Sunday April 10, 2022 @04:35PM (#30981435)
I just saw a tweet from Esteban Küber[0] the other day that made me rethink a silly thing I did:

"I started learning Rust in 2015. I've been using it since 2016. I've been a compiler team member since 2017. I've been paid to write Rust code since 2018. I have needed to use `unsafe` <5 times. That's what why Rust's safety guarantees matter despite the existence of `unsafe`."

For me, I was being a bit lazy with loading some config on program startup that would never change, so I used a `static mut` which requires `unsafe` to access. Turns out I was able to figure out a way to pass my data around with an `Arc<T>`. I think either way would have worked, but I figured I should avoid the unsafe approach anyway.

[0] https://twitter.com/ekuber/status/1511762429226590215

Re:What Is Rust's Unsafe? (2019) by oconnor663 on Sunday April 10, 2022 @04:41PM (#30981488)
Yeah if you don't need mutation after initialization is done, Arc<T> is a good option for sharing. Lazy<T> (https://docs.rs/once_cell/latest/once_cell/sync/struct.Lazy....) is also nice, especially if you want to keep treating it like a global. I believe something like Lazy<T> is eventually going to be included in the standard library.
Re:What Is Rust's Unsafe? (2019) by jamincan on Sunday April 10, 2022 @07:09PM (#30982681)
Re:What Is Rust's Unsafe? (2019) by afdbcreid on Sunday April 10, 2022 @11:33PM (#30984315)
Don't ever use `static mut`. It's dangerous[1]. In nightly you now have `SyncUnsafeCell`[2] to avoid it.

[1] https://github.com/rust-lang/rust/issues/53639

[2] https://github.com/rust-lang/rust/pull/95438

Re:What Is Rust's Unsafe? (2019) by shepmaster on Sunday April 10, 2022 @04:46PM (#30981534)
I’ll be even lazier (heh) and just straight up leak memory for true once-set read-only config values.

https://github.com/shepmaster/stack-overflow-relay/blob/273a...