News for hackers. Stuff that matters.
Re:The Case for Rust in the base system by hartator on Sunday January 21, 2024 @01:47PM (#39081478)
Very naive question. What's the main selling point of Rust besides being stably sponsored by Mozilla?
Re:The Case for Rust in the base system by oconnor663 on Sunday January 21, 2024 @02:06PM (#39081717)
There are two very broad set of features:

1) Memory safety. If you live in the world of kernels and embedded code, your options are mostly C, C++, and (as of just the last few years) Rust. Of those, only Rust reliably prevents memory corruption mistakes like use-after-free.

2) Being a 21st century language. Rust has a lot of features that any new language would be expected to have these days: a unified build system, a library ecosystem that the build sytem knows about, slices over arrays and vectors, UTF-8 strings, convenient ways to de/serialize JSON, something like async/await, etc. This makes it competitive with languages like Java and Python for doing "everyday stuff", where realistically ~no one would use C. The main downside of Rust in a Java/Python sort of context is that the learning curve is a lot steeper.

A couple other notes:

- Some of nice 21st century features (like JSON) go away when you're writing embedded/kernel code, but others still work. Embedded ("no_std") Rust still has enums, error handling, slices, iterators, and sometimes even async/await. Those quality-of-life features are hard to replicate in C, even if you believe your code is 100% bug free :)

- If you know you're going to be writing multithreaded code, Rust's thread safety features are really unmatched. A lot of the same machinery that gets used for memory safety also turns out to be useful for thread safety. Libraries like Rayon make it surprisingly convenient to write multithreaded code, while also catching a huge percentage of mistakes at compile time. (Deadlocks are still possible though.)

Re:The Case for Rust in the base system by hartator on Sunday January 21, 2024 @02:12PM (#39081788)
> Memory safety

Another very naive question. Are memory safety concerns from a cybersecurity standpoint or from less-dev error standpoint?

Re:The Case for Rust in the base system by brobinson on Sunday January 21, 2024 @02:26PM (#39081973)
Microsoft, Google, and other companies have all determined that about 70% of CVEs are memory-related. Here's the US government's take: https://www.cisa.gov/news-events/news/urgent-need-memory-saf...

These memory-related exploits disappear with Rust aside from in "unsafe" blocks (possibly the worst named keyword in any language... it should have been called "trusted"), and that means you have a smaller and more easily auditable attack surface for these types of memory-related exploits. Some code (e.g., FFI) can't be verified as memory-safe by the Rust compiler at compilation time so "unsafe" is there as an escape hatch. I've written a bunch of Rust since 2014 building things like webservers, realtime futures processing algorithms, MEV bots, etc., and I've only had to use "unsafe" a few times.

I've also worked in security on products at Fortune 100 companies, and C is a constant nightmare for CVEs. I think I have PTSD from having to update libcurl. The more software we have written in memory safe languages, the better off we all are.

Re:The Case for Rust in the base system by wredue on Sunday January 21, 2024 @04:26PM (#39083244)
Just to avoid any “cyclist rides more aggressively when wearing helmet” type behaviour:

It’s probably worth nothing that that vast majority of, and of large scale attacks, is basic shit that has nothing to do with memory safety.

There’s benefit to memory safety, but just cause you’re wearing a helmet doesn’t mean you’re completely safe.

Additionally, it’s not Google overall, but chromium project that find this, as well as not Microsoft overall, but specifically Windows. These are two projects that will naturally see a much higher rate of memory safety issues.

Re:The Case for Rust in the base system by steveklabnik on Sunday January 21, 2024 @07:27PM (#39084567)
> not Microsoft overall, but specifically Windows

This isn't the case: https://msrc.microsoft.com/blog/2019/07/a-proactive-approach...

> Since 2004, the Microsoft Security Response Centre (MSRC) has triaged every reported Microsoft security vulnerability.

They've never qualified it as Windows only, to my knowledge.

Re:The Case for Rust in the base system by oconnor663 on Sunday January 21, 2024 @02:25PM (#39081967)
Both for sure, and which one is a bigger deal depends a lot on context.

Cybersecurity issues make Rust a good choice for writing parsers for example. File format parsers handle scary data from the internet (JPEG, X.509, etc), and those often contain addresses and offsets that turn into raw pointers in the implementation, so they're a common source of security bugs. At the same time, they're usually isolated behind a reasonably tight API, so it can be practical to rewrite them in Rust even when all the calling code is still C or C++ or even Python.

On the other hand, fewer-mistakes-sucking-up-dev-time is an especially big deal in multithreaded code. There was a story from Mozilla about how they'd tried to add threading to part of Firefox (I think it might've been the CSS engine?) and failed multiple times, but then they succeeded in Rust. Threading bugs tend to be painful to reproduce, and Rust's compile-time checks are extremely valuable there.

Re:The Case for Rust in the base system by logicprog on Sunday January 21, 2024 @02:57PM (#39082358)
Both. On the one hand, what Rust essentially forces you to do is just to program according to proper RAII principles that an experienced C++ programmer would already know and should already be using (especially once polonius lands), it's just better at checking failure to do so than any sanitizer or linter could ever be, so it's a way to avoid having to deal with annoying, hard to track down memory bugs. And in fact, the greater confidence static lifetime guarantees give you allows you to do things like using move semantics more where C++ programmers would have to use copies out of defensive programming. That's predominantly why I personally use it, as far as memory safety goes. On the other hand, [around 60-70% of serious security bugs have been proven to be caused by memory safety across many different projects](https://alexgaynor.net/2020/may/27/science-on-memory-unsafet...) so if you do care about security in what you're doing, it's a boon there too.
Re:The Case for Rust in the base system by kstrauser on Sunday January 21, 2024 @04:39PM (#39083342)
Tomato, tomato.
Re:The Case for Rust in the base system by eloisant on Sunday January 21, 2024 @02:18PM (#39081884)
Mostly from a less-dev error standpoint but I think you gain in cybersecurity as well, as the language won't let you write code that accesses memory in places it's not supposed to.
Re:The Case for Rust in the base system by masklinn on Sunday January 21, 2024 @02:29PM (#39082008)
It's mostly from the cybersecurity standpoint actually, at least for large projects / companies. MS, Chromium, Project Zero, Mozilla, have pretty consistently found that north of 70% of vulnerabilities were rooted in memory safety.

The dev-error is broader language guarantees, which dovetail into memory safety but are useful on their own e.g. data race safety, rich type system, ...

Re:The Case for Rust in the base system by CharlesW on Sunday January 21, 2024 @02:21PM (#39081918)
Both, and they're related in the sense that software errors can become exploitable vulnerabilities. This is a handy summary by the NSA: https://media.defense.gov/2022/Nov/10/2003112742/-1/-1/0/CSI...
Re:The Case for Rust in the base system by jcranmer on Sunday January 21, 2024 @02:32PM (#39082044)
I can't give you just one selling point, because Rust has two main competitive features, and it's hard for me to say which one is enticing FreeBSD more here:

1. Rust has a more modern sense of computer primitives than C does. For example, C has the char-short-int-long-long long set of types which correspond to 8, 16, 32, and 64 bit integers in practice (and yes, 5 types for 4 sizes), whereas Rust has u8/u16/u32/u64. Rust also has a builtin slice type, which is a desperately needed notion of pointer + size of array, and C's lack of such type has proven to be the source of much malware. The standard library is also somewhat richer too, you have better support for things like threads or networking than C/C++ does. But note that Rust is still like C in that it has a very thin runtime layer, and can also go a step further and have "no" standard library at all, which is necessary in several contexts.

2. The borrow checker. Effectively, use-after-free (or any other lifetime issue) vulnerabilities become compiler errors, and even many kinds of data races are also turned into compiler errors. Also it can do things like make iterator invalidation issues a compiler error as well.

Re:The Case for Rust in the base system by eropple on Sunday January 21, 2024 @01:48PM (#39081488)
In short: the type system is significantly more expressive and encourages domain modeling that tends to lead to fewer errors; "if the compiler says it's OK, it'll be OK" isn't strictly true but it's often pretty close. Rust also offers stronger guarantees around memory safety than C, as well as C++ as commonly practiced. (Which is to say, you can write very memory-safe C++, but you will work at it.)
Re:The Case for Rust in the base system by logicprog on Sunday January 21, 2024 @03:08PM (#39082480)
One of the biggest things about Rust for me is that it's essentially Prometheus, bringing the fire of modern languages and more advances PL theory to systems programming, which imo is sorely needed. We don't need a 10% better C or C++, we need a huge jump for the field. Rust I think does this. They have:

1. A type system that's deeply inspired by OCaml, a language that's designed and used by a lot of programming language research people and is widely praised (just look at how every other language — C#, Java, Kotlin — is suddenly trying to pile half baked imitations of ML features like ADTs and deep pattern matching onto their existing OOP type systems), except they've both imemented the type system correctly and fully, and also filed off all the warts and awkward features, and replaced OCaml's verbose and underpowered first class module system with Haskell style type classes

2. got amazing, highly flexible and generalized iterator and monad support (you can use iterator methods on monads), basically second only to Haskell itself, that compiles down to the equivalent of hand written assembly code loops

3. an error handling system using monads that neatly sidesteps the issues with both traditional C style error handling and exceptions that combines with point two in an exponential curve of awesomeness for error handling

4. hygienic (from Racket) and procedural (eg Common Lisp) macros

5. And finally ofc their statuc memory safety and race condition safety, which is based on linear types ala Idris.

But most importantly, Rust's designers seem to IMO have carefully picked only the advanced programming language features that tend to make your code clearer as well as more correct, and help you model problems at the right level of abstraction to be intuitive, and also only features that can be implemented in a way that is as performant as the equivalent C++ code, roughly speaking at least. They also seem to have been very careful with how they integrate and implement everything in order to uphold those goals.

So you get to have 90% of the power of a language like Haskell, in a language that performs like, and has the low level capabilities of, C++, with the extra benefit of — for instance — less dogmatic puritanism and more deterministic and comprehensible performance and execution properties then Haskell, and less Lovecraftian horror full of surprise gotchas than C++.

It's kind of like the ultimate pragmatic language for someone like me who has gone deep into pure functional languages and other esoteric languages and wants to have as much as of that goodness as is practical.

Re:The Case for Rust in the base system by tialaramex on Sunday January 21, 2024 @05:24PM (#39083707)
By the way it's not "race condition" safety. Race conditions are a completely normal part of our world and so Rust can't magically fix race conditions. Bob's going to shred all "10+ year old" paper records "after lunch" and Hannah needs the paper customer list from 2003 for the 2:30pm meeting? Well that's a race, good luck, Rust can't help. However, (safe) Rust can eliminate data races, a very weird special case caused by the difference between how you imagine the computer works and how it actually works.

A data race goes like this: At least two simultaneous execution contexts (maybe threads for example) are looking at the same object X and at least one of them changes it, but there is no particular order of these events agreed between these contexts.

In your head, even with parallel computing everything seems to happen in some sort of global order. A happens before B, or B happens before A. This is called Sequential Consistency, and it's a lie, the machine doesn't actually work like that, but humans can't really understand non-trivial software without this lie, so, all our high level software (and when I say "High level" I mean like the C Programming Language) pretends sequential consistency is always preserved and goes to some lengths to achieve that.

In Rust you can go about your business. (Safe) Rust promises this is true and it'll make damn sure. But in many languages like C or C++, actually you can very easily inadvertently construct a data race, revealing that it was a lie and if you do so all bets are off. Since you probably can't reason about your program's behaviour anyway, they figure "fuck it" and that's Undefined Behaviour.

Bonus round for languages which do better than most: Go says if you race a trivial object like an integer, you lose Sequential Consistency but this isn't automatically UB. Complex races are UB.

Java says races are never UB, but they do lose Sequential Consistency. Your Java Program is now very, very difficult to understand, but it's not nonsense.

OCaml goes furthest, it says your race isn't UB and it offers very tight constraints on what's wrong. OCaml's work on this is relatively new, so it may be a while before we're confident whether this is a manageable situation normal humans (well OCaml programmers) can handle.

Re:The Case for Rust in the base system by logicprog on Sunday January 21, 2024 @06:18PM (#39084075)
> However, (safe) Rust can eliminate data races,

Yes, thank you for pointing that out, ive had a bad headache for most of the day and knew I was saying the wrong term when I wrote it, but couldn't for the life of me remember what the right one was! And yeah I'm pretty familiar with this, although I wasn't aware of OCaml's strides

Re:The Case for Rust in the base system by kagakuninja on Sunday January 21, 2024 @06:40PM (#39084225)
>2. got amazing, highly flexible and generalized iterator and monad support (you can use iterator methods on monads), basically second only to Haskell itself, that compiles down to the equivalent of hand written assembly code loops

As I understand it, Rust lacks do-notation and higher-kinded types, making it hard to have true monads in Rust.

Scala would be the language with monad support comparable to Haskell.

Re:The Case for Rust in the base system by nicoburns on Sunday January 21, 2024 @07:28PM (#39084581)
My understanding is that Rust lacks the ability to have a generic Monad abstraction like Haskell and Scala, but that it certainly has monads (e.g Option, Result, Vec, etc). And indeed that even languages like JavaScript have lots of monads (e.g. Promise) it's just that these are likewise not abstractable.

I believe that a monad is basically any container that has both a constructor and a flat_map function (and there may be one other requirement that I've forgotten). The special ability that Haskell has is the ability to write code that is generic over any such container.

Re:The Case for Rust in the base system by nyssos on Monday January 22, 2024 @02:06PM (#39093839)
> (and there may be one other requirement that I've forgotten)

`map: forall a b . (a -> b) => (m a -> m b)` and `pure: forall a . a -> m a`, plus some (mostly very natural) compatibility conditions between the three: `flatMap(pure)` does nothing, `map` takes the identity to the identity, etc.

Re:The Case for Rust in the base system by logicprog on Sunday January 21, 2024 @07:06PM (#39084436)
I actually had this objection in mind when I wrote that, so thank you for giving me the opportunity to expand on what I meant a little bit.

Yes, it is very much true that rust cannot yet represent monads as type classes directly, since it does not have any form of higher kinded types yet — which is the reason why I said second only to Haskell. If it could represent monads directly, than I would have placed it equal to Haskell — as I would have placed scala!

But I do think it is important to highlight that it does have pervasive useful monad use in the language, and an interface that allows you to use a very large common set of powerful transformations on them via the iterator trait, and it uses the monads that exist in the language and the ability to use that trait on any monad a lot to increase the power of the language in a way no other language besides ones with superior monad support does that I know of. So I would argue that while it is still second to Haskell, it is still far above almost any other language not equal to Haskell in this respect. Which is what I meant by saying second only to haskell!

Also, HKTs are something that the rust team seems to be very actively looking to rectify with an in progress implementation currently available in nightly, so that won't be true for very long.

Re:The Case for Rust in the base system by nyssos on Sunday January 21, 2024 @06:46PM (#39084263)
> OCaml's verbose and underpowered first class module system

It's definitely verbose, but underpowered? Modules are just as powerful as Haskell's typeclasses (and therefore more powerful than Rust's).

Re:The Case for Rust in the base system by logicprog on Sunday January 21, 2024 @07:00PM (#39084393)
It's certainly debatable. This is just my opinion.

In my opinion they are, because any group of types and functions that implements a module signature must be wrapped up into a module that satisfies that signature at the point of use of any value that needs to satisfy that module signature, instead of a type just satisfying the given signature if the correct functions exist to manipulate it, which means in my opinion there's a lot less flexibility and it works more like constructing a new value of a certain type then it does having constrained parametric polymorphism in the way Haskell does. I think this is why although they technically can be used to imitate rust traits or Haskell type classes and allow a function to polymorphically take any type of that satisfies a certain interface, it isn't in practice used like that almost at all, whereas it absolutely is in Rust and Haskell. Witness for instance how both rust and Haskell have a generic type class for iteration that allows you to take anything that is iterable and use the whole module of functions available for iterables on it, so for instance in Rust you can use many of the same methods on a vector and an array and error checking monads, whereas in ocaml, even though something similar is in principle possible, it's almost never done that I've seen, because you have to essentially declare that I type implements a signature and how it does on every point of use where you want to pass a value of that type into a function that requires something with a certain signature. Instead functions are duplicated between modules for different types, like lists and arrays. Maybe that isn't underpowered per se, but it's certainly a practical consideration in the use of first class modules that mitigates a lot of what they are theoretically capable of that seems in my opinion to fall directly out of the conceptual model of using first class modules to do constrained parametric polymorphism, since values then must become modules.

As for the ability to represent the signature of monads — yes both ocaml and Haskell can currently do that while rust cannot, but there is actually currently active work on an RFC that would rectify that. So I suppose overall you might say that rust's type system is currently less powerful then the other two, but I really don't think that will be true over the long run, and it certainly isn't true of necessity, and rust has a type system with a great many benefits in practical power over OCaml's otherwise, in ways I feel impact day to day use more.

Re:The Case for Rust in the base system by logicprog on Sunday January 21, 2024 @07:14PM (#39084489)
Errata:

Second only in monad support to Haskell and equivalently powerful languages e.g. OCaml and Scala, but much better than mainstream languages, I should've said.

Also, I should have said that Rust helps with data races not races in general.

Also, I should've said Rust's trait system was more practically powerful / usable that OCaml's first class modules and potentially more powerful overall but not quite there yet.

This is what comes with writing enthusiastically, while suffering from a headache and brain fog. I swear I'm not an idiot and know my pure functional languages!

Re:The Case for Rust in the base system by logicprog on Monday January 22, 2024 @09:21AM (#39089787)
My point here wasn't to say that Rust is as good or powerful as the advanced pure functional languages like Haskell and Ocaml and Idris, but that it's starting the process of properly bringing their power and features to the systems programming world, which I think is what's important.
Re:The Case for Rust in the base system by adrianN on Sunday January 21, 2024 @02:04PM (#39081690)
Rust‘s type system prevents memory safety bugs because it reasons about object lifetimes statically. At the same time rust allows you to write systems code on bare metal.
Re:The Case for Rust in the base system by dosshell on Sunday January 21, 2024 @02:10PM (#39081762)
> it reasons about object lifetimes statically.

How does that differ from RAII?

I think i misunderstand you or lack knowledge, because this sounds exactly like RAII.

I know that rust has major compile time checks, but saying that the difference is that it reason about life time as difference to c++ is misleading. I think the major point of c++ compared to c is that c++ "reason about object lifetime statically" with deconstructors and RAII. And saying that rust do this and implying c++ don't is misleading.

Re:The Case for Rust in the base system by oconnor663 on Sunday January 21, 2024 @02:13PM (#39081815)
Rust and C++ work very similarly with respect to objects you own. In both languages they get cleaned up by a destructor when they go out of scope. However, Rust also has a lot of language features that deal with objects you borrow, i.e. have a pointer to. In C++ you might make a "use-after-free" mistake and hold a pointer to an object that's already been destructed, which can lead to all sorts of memory corruption. In Rust, the same mistake almost always results in a compile-time error. The part of the compiler that enforces all this is called the "borrow checker", and getting used to the borrow checker's rules is a big part of Rust's learning curve.

One thing C++ programmers might be interested to learn, is that this doesn't only apply to simple variables and references; it also applies to library data structures and their methods. The Rust compiler doesn't really "know" what the .clear() method on a Vec does, but it knows enough to prevent you from calling that method while you're holding references to the Vec's elements.

Re:The Case for Rust in the base system by baq on Sunday January 21, 2024 @02:33PM (#39082050)
> getting used to the borrow checker's rules is a big part of Rust's learning curve.

It’s also a big part of C and C++’s learning curves, it’s just that the compiler doesn’t tell you about it; you’re being taught by segfaults and silent memory corruption instead.

Re:The Case for Rust in the base system by oconnor663 on Sunday January 21, 2024 @04:11PM (#39083109)
Very true. My hot take on this is, if you want to learn C or C++ "properly" (not just well enough to get your homework done, but well enough to write medium-size programs that pass ASan/UBSan/TSan), the fastest way is to learn Rust first. This is emphatically not because Rust is quick to learn, but rather because ownership and borrowing discipline takes years of mentorship to absorb in C and C++, and plenty of career programmers never really get it.
Re:The Case for Rust in the base system by zozbot234 on Sunday January 21, 2024 @02:34PM (#39082066)
> Rust and C++ work very similarly with respect to objects you own.

There is one major difference. In Rust you can memcpy objects you own to a different base address and they will still work, unless they're pointed at by a Pin<> type (as such, code that requires objects to stay put in memory must take a Pin<> reference. This arrangement may potentially be replaced by a special "?Move" trait in future editions of Rust). C++ pins all objects by default and allows objects to have custom "move" constructors, to sort of enable them to be moved elsewhere.

In C++, objects that get "moved" must leave behind something that can be destructed cleanly; Rust has no such limitation, at least wrt. ordinary moves. Arguably the closest thing it has to a C++ move is the .take() pattern, which leaves a default-constructed object behind. But this is rarely used.

The general tradeoff is that the Rust pattern memcpy's compound objects more often, but has less gratuitous use of the heap and less pointer chasing compared to idiomatic C/C++. This is a valid choice once one has the compiler support that Rust provides.

Re:The Case for Rust in the base system by oconnor663 on Sunday January 21, 2024 @02:39PM (#39082135)
If anyone's trying to follow along but wondering what the heck we're talking about, I have a video about this :) https://youtu.be/IPmRDS0OSxM?t=3019
Re:The Case for Rust in the base system by masklinn on Sunday January 21, 2024 @02:36PM (#39082093)
> However, Rust also has a lot of language features that deal with objects you borrow

An other major safety difference is that Rust uses destructive moves, once a binding is moved-from it becomes invalid / inaccessible (and it won't be destroyed).

In C++, a moved-from object is in a "valid but unspecified state", so it must be destructible (because destructors always run) but any interaction with the object other than destruction may be UB, and the compiler won't tell you (in the same way it won't tell you about borrowing issues whether it's UAF, invalidation, ...).

Re:The Case for Rust in the base system by adrianN on Monday January 22, 2024 @01:44AM (#39086989)
Yeah if you never use a pointer/reference/iterator, the guarantees are quite similar. The rust compiler however also checks for use after free and iterator invalidation bugs. In addition it makes it harder to shoot yourself in the foot in concurrent code because you can’t have multiple mutable references to an object.
Re:The Case for Rust in the base system by chaxor on Sunday January 21, 2024 @01:49PM (#39081507)
The build tools are incredible. That's the main reason many people love Rust. For kernel dev however, the memory safety is a big win.
Re:The Case for Rust in the base system by paulddraper on Sunday January 21, 2024 @01:50PM (#39081516)
Statically checked memory management

Advanced typing

Build tools

Re:The Case for Rust in the base system by formerly_proven on Sunday January 21, 2024 @02:06PM (#39081707)
I don’t think Mozilla has had any major sponsorship role for a few years now?
Re:The Case for Rust in the base system by hartator on Sunday January 21, 2024 @02:32PM (#39082042)
> https://internals.rust-lang.org/t/mozilla-is-hiring-for-the-...

It seems that some Rust folks are still on Mozilla payroll.

Re:The Case for Rust in the base system by steveklabnik on Sunday January 21, 2024 @03:52PM (#39082921)
That post is from 2019, the layoffs of the teams were in 2020.

Mozilla is still a member of the foundation, so they do support it in that sense, but they are one of many companies that do so.

Re:The Case for Rust in the base system by avgcorrection on Sunday January 21, 2024 @02:04PM (#39081692)
The language features.
Re:The Case for Rust in the base system by secondcoming on Sunday January 21, 2024 @02:17PM (#39081875)
You've got to be trolling