Hacker News new | ask | show | jobs
by oconnor663 2691 days ago
> They make it surprisingly difficult to say "hey, read these 10 bytes, its this struct".

It's really easy, but it's `unsafe`:

    let my_bytes = [...];
    let my_struct = unsafe {
        std::mem::transmute(my_bytes);
    };
It's necessarily unsafe, because Rust has no way of knowing what invariants the struct is responsible for upholding. If the struct contains a Vec, for example, then transmuting it from bytes will probably give you garbage pointers and a security vulnerability.

I think something very interesting has happened with `unsafe` in the Rust community as the language has grown. There are lots of things that are "easy with `unsafe`", but everyone seems to round that up to "hard". I think that's a Very Good Thing, because it means that safe code is powerful enough and convenient enough that not using it is seen as a big deal.