4 ms·
As mentioned in the article, it's entirely possible to create a raw pointer to an object in safe rust, you just can't do much with it. One thing you can do with
by EE84M3i 5y ago
As mentioned in the article, it's entirely possible to create a raw pointer to an object in safe rust, you just can't do much with it. One thing you can do with it though is convert it to an integer and reveal a randomized base address, which isn't possible in some other languages without their "unsafe" features. Of course, this follows naturally from Rust's definition of what is safe, but I remember being kind of surprised by it when I first learned Rust and didn't understand those definitions yet.
It would be pretty interesting to me if someone wrote a survey of what different languages consider to be "unsafe", including specific operations like this. For example, it looks like "sizeof" is relegated to the "unsafe" package in Go, which strikes me as strange.[1] I'd love to read a big comparison.
[1]: https://pkg.go.dev/unsafe#Sizeof https://pkg.go.dev/unsafe#Sizeof
- afdbcreid 5y agoYou can also compare & hash it. I once used it, to store visited objects in a hashset without the borrow checker making me sad.
- afdbcreid 5y agoI am by no means a Go expert, but I think the `unsafe` package is the only standard library package in Go that does not conform to the stability & portability guarantees and `Sizeof` is neither portable nor stable.
- EE84M3i 5y agoAt the top of the page it says "Package unsafe contains operations that step around the type safety of Go programs." though