Live data from Hacker News

Uninitialized memory: Unsafe Rust is too hard

lucumr.pocoo.org

1–10 of 127 posts

Re: Uninitialized memory: Unsafe Rust is too hard

#3

I don't know rust, but why isn't the answer, don't try to do what you'd do in C like construct uninitialized structs?

They are pretty useful for a number of data structures, and Rust uses them heavily in the standard library.

I'm not aware of as many uses of field by field initialization of a struct but there is an example similar to this blog in the docs[1] (without the alignment considerations.)

That said my read has been the complexity is accidental as a result of language decisions to improve safe rust. MaybeUnit was only defined 3 years ago when it was discovered that mem::uninitialized /zeroed resulted in undefined behavior when used with bools. [2][3]

[1] https://doc.rust-lang.org/std/mem/union.MaybeUninit.html#ini...

[2] https://github.com/rust-lang/rust/issues/53491

[3] https://doc.rust-lang.org/std/mem/fn.uninitialized.html

Re: Uninitialized memory: Unsafe Rust is too hard

#7
I am under the impression that even _safe_ Rust is really hard to learn. Several years ago I started with GoLang and it was so easy to start programming even advanced things almost instantly..Rust drives me crazy. The syntax seems overcomplicated, the compiler errors are cryptic, the IDE is not helpful.

Re: Uninitialized memory: Unsafe Rust is too hard

#8

What is the reason for the rule objects have to be always in a good state even inside unsafe?

One of the core ideas behind unsafe blocks is that they don’t actually change any semantics. All they do is allow more operations. This makes it a lot easier to reason about (for both the programmer and the compiler) since there’s not two different set of rules to remember.

It does however makes things a bit clunky since the unsafe bits need to ensure a “safe” state throughout the whole block and not only by the end of it.

Re: Uninitialized memory: Unsafe Rust is too hard

#9
Without any unsafe code this is simply:

    let role = Role {
        name: "basic",
        flag: 1,
        disabled: false,
    };
The language tries to prevent you from interacting with a `Role` object that's not fully initialized. `mem::zero()` could work, but then you'll have to turn the `&'static str` into an `Option` or a raw pointer, to indicate that it might be null. You could also add `#[derive(Default)]` to the struct, to automatically get a `Role::default()` function to create a `Role` with and then modify the fields afterwards, if you want to set the fields in separate statements for some reason:

    let mut role = Role::default();
    role.name = "basic";
    role.flag = 1;
    role.disabled = false;
And even with `MaybeUninit` you can initialize the whole struct (without `unsafe`!) with `MaybeUninit::write`. It's just that partially initializing something is hard to get right, which is the point of the article I guess. But I wonder how commonly you would really want that, as it easily leads to mistakes.

Re: Uninitialized memory: Unsafe Rust is too hard

#10

I am under the impression that even _safe_ Rust is really hard to learn. Several years ago I started with GoLang and it was so easy to start programming even advanced things almost instantly..Rust drives me crazy. The syntax seems overcomplicated, the compiler errors are cryptic, the IDE is not helpful.

> I am under the impression that even _safe_ Rust is really hard to learn.

[...]

> The syntax seems overcomplicated, the compiler errors are cryptic, the IDE is not helpful.

Yes, Rust is hard to learn. Rust does _seem_ over-complicated.

However I like to compare Rust to exercise. You need to do a bit of it before you start reaping the benefits of it.

If you suspend your judgement for a bit and try to write some Rust, starting from the very beginning you will find that:

- Rust is actually a small language at its core, unlike the monstrosity that is C++ . You don't really need Advanced Rust to be productive. Use Advanced Rust only when you're... advanced

- Rust actually is very consistent

- The Rust compiler is actually very helpful. It's the least cryptic compiler I've met. But its OK if you feel that now as you're just beginning your journey with Rust

Avoid the temptation to "read" Rust from a book. Try to _do_ Rust. Otherwise it might overwhelm you. Simply keep adding Rust techniques to your arsenal as you mature in your usage of Rust.

Learning Rust changed the way I look at programing. Rust is a beautiful language. As a random example, just look at the the Firecracker VMM written in Rust -- https://github.com/firecracker-microvm/firecracker . It would have been able to very difficult for me to understand the codebase if it were written in C/C++!

Rust is one of those rare languages I've encountered that if the code compiles, there is a high probability it will work. The type system is that good!

TL;DR Persist and you will reap the rewards with Rust.

Post reply on HN