3 ms·
Rust doesn't have nil/null values. It has an Option type that enforces at compile time that you explicitly handle missing values, through pattern matching, tran
by p-e-w 4y ago
Rust doesn't have nil/null values. It has an Option type that enforces at compile time that you explicitly handle missing values, through pattern matching, transformations, or unwraps.
This makes 95% of the problems associated with nil/null pointers magically disappear. The difference this makes for the safety of large-scale systems is huge.
- lelanthran 4y ago> Rust doesn't have nil/null values. Yeah, and it's also not popular. The reason I said "popular" is because the popular languages are addressing pain-points for devs. If nil/null was a pain point, none of the popular languages would allow nil/null values. The simple fact is that, for many developers, there is a pressing need to indicate lack of a value. Languages that don't address this pain-point never really get taken up en masse.
- ghqst 4y agoI don't think anyone gets to say that Rust isn't popular anymore. Especially considering it's now in the Linux kernel.
- cesarb 4y agoPlaying devil's advocate: what's now in the Linux kernel is the infrastructure to allow writing code in Rust, but AFAIK for now nothing uses it (there's Asahi Lina's Rust driver for the Apple M1/M2 GPU, but it's still out-of-tree). I find it likely that Rust in the Linux kernel will not be used for anything important (other than architecture-specific code) until either it can be compiled using GCC (either the GCC backend for the Rust compiler, or the Rust frontend for the GCC compiler), or the LLVM backend supports every single architecture supported by the Linux kernel (or the Linux kernel loses support for the architectures the LLVM backend doesn't support).
- pjmlp 4y agoAndroid fork of Linux kernel uses it. Bluetooth stack was rewritten in Rust for Android 12, and the new virtualization stack in Android 13 is based in crosvm, also a Rust.
- hota_mazi 4y agoAnd there are 1.5M lines of code of Rust in Android [1]. [1] https://www.theregister.com/2022/12/02/android_google_rust/ https://www.theregister.com/2022/12/02/android_google_rust/
- lelanthran 4y ago> I don't think anyone gets to say that Rust isn't popular anymore. Especially considering it's now in the Linux kernel. Popular amongst Rust fans, certainly. Popular amongst employers, no. It's still barely a rounding error for employers.
- the_duke 4y ago"Old language have it, so it must not be a problem" is not a very convincing argument. Almost every new language that has come out in the last ~10 years or so tries hard to avoid null pointers, and old languages try very hard to mitigate the problem in various ways. Dart went through a tedious process of removing nullability. Kotlin doesn't have nullability (well, sort of, using Java bindings can still get you null pointer exceptions). There are lot's of attempts for Java, from static analysis tools (eg recently from Meta , but there are older solutions), Springs new @NonNull, etc. C# switched to making non-nullable the default a few years ago. Rust only has null pointers in the unsafe part of the language, and only for low level raw pointers, not for traits. Go is the odd one out.
- lelanthran 4y ago> "Old language have it, so it must not be a problem" is not a very convincing argument. That strawman is not the argument. The argument is that "popular languages have it, so it must be solving some problem". You can say what you like about popularity, but it still remains a good indicator of what works in practice and what doesn't, especially when the alternatives are all well-known and have been around for decades.
- lairv 4y agoYou are playing with words, Rust / OCaml Option types are effectively the same as nulls in null-safe language, like `string | null` in TypeScript or `string?` in Dart, although I agree it has become necessary for such things to be checked at compile-time
- tialaramex 4y agoThe difference is crucial. With these Sum types a variable of type Option<Dog> / Dog | null / Dog? might be a dog, but a variable of type Dog is definitely a dog and the language has both of those types. Languages which just have one nullable type here like Go must use their "might be a dog" type to represent things which definitely are a dog, meaning either you write paranoid checks everywhere or you might screw up - and it's no wonder there are more bugs as a result.
- cesarb 4y ago> Rust doesn't have nil/null values. I have to nitpick here: Rust raw pointers (https://doc.rust-lang.org/std/primitive.pointer.html https://doc.rust-lang.org/std/primitive.pointer.html which does note "Working with raw pointers in Rust is uncommon") do have null values. It's not usually a problem since raw pointers can only be dereferenced in unsafe code; most of the time, you will be using a reference wrapped with an Option, which has the same size and same possible values (the compiler knows a reference is never null, so it uses the "missing" null value to represent the None variant of the Option enum).
- Lev1a 4y ago> since raw pointers can only be dereferenced in unsafe code Sure, and "unsafe" is not only a huge red flag but can and should be tightly scoped and can then be audited much easier than chasing around NPE through an entire Class or even codebase. Secondly, I've used Rust for a few years now and have NEVER needed to or thought of using raw pointers once. It's unnecessary for almost every single usecase I can think of except maybe OS or driver development but even then you can tightly control where "unsafe" uses pop up.