3 ms·
> Rust’s raw pointer types are nullable by default and have no null-pointer dereference checking. There is a NonNull<T> pointer type that gives you more safety.
by nextaccountic 2y ago
> Rust’s raw pointer types are nullable by default and have no null-pointer dereference checking. There is a NonNull<T> pointer type that gives you more safety. I’m not sure why it’s not the default, since Rust’s references are non-nullable.
NonNull<T> would probably be a better default indeed, but one reason might be that NonNull<T> has more UB than *mut T (namely, it is UB for it to be the null pointer).
(A lot of the text is about how more UB makes unsafe programming harder, which is a fair point)
> Zig pointers, by default, are non-null and you opt-in to nullability using the ? syntax (e.g. ?*Value). And of course, you get null pointer dereference checking enabled by default too.
Well so Zig pointers, by default, has more UB than Rust pointers, at least when concerning null pointers.
- throwawaymaths 2y agothere is no UB in zig pointers in ReleaseSafe compilation mode.
- hansvm 2y ago> Well so Zig pointers, by default, has more UB than Rust pointers, at least when concerning null pointers. Not quite. In Zig, a (non-C) pointer type is closer to a Rust reference in its behavior. It isn't null, and that's enforced by the type system. The author's point about opting into nullability is that in a function signature you can choose to allow null pointers to be passed to the function by making the argument type an optional pointer, a weaker type with the same safety guarantees (because, like with Rust, the type system forces you to handle the null branch).