Article View: comp.arch
Article #8Re: Rust (was: Optimization by programmers)
From: Dan Cross
Date: Wed, 07 Oct 2026 14:23
Date: Wed, 07 Oct 2026 14:23
137 lines
7352 bytes
7352 bytes
In article <2026Oct6.172109@mips.complang.tuwien.ac.at>, Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote: >[snip] >My impression is that in Rust you tend to nail down things more often >than in Haskell, so the stretches are shorter, but there is more >automagic going on in Rust, with automatic defererencing and >referencing, automatic dropping, etc., and the borrow checker >interacts with the automagic, which makes it at least as hard to deal >with as th Haskell type inference. The rules for when references are taken, when dereferences happen, and when things are dropped are well-defined; there's no magic there. To a first order approximation, a references is taken using the `&` and `&mut` operators, or when an object is the target of a method call that takes `&self` or `&mut self`. An object is dereferenced using `*` operator, the `.deref()` operator for something that impl's the `Deref` trait, or when using the `.` operator on product types. An object is dropped either when it goes out of scope, or when subject to the `Drop` operator. Rust objects and references have lifetimes, which are a first-class part of the type system. The borrow checker enforces rules relating the lifetimes of objects and their references. Specifically: * A reference to an object cannot outlive the object itself (no dangling references). * If a _mutable_ reference to an object exists at some point in time, then that reference is the only reference to that object. * Any number if _immutable_ references may exist to an object at any given time, mutually exclusive with a mutable reference to that object. Rust references are non-nullable, and the borrow checker rules enforce that they always point to a valid object; the language will not allow you to take a reference to an uninitialized object. There are a couple of fine points here; if you have a mutable reference to something, it's ok to call a method on it that takes `&self`, for example, but for the most part the above is a decent mental model. There are also so-called "raw" pointers in Rust that are similar to pointers in C or Pascal in that they are nullable, can dangle, and don't necessarily have to point to a valid, initialized object. One can create a raw pointer using the `&raw` or `&raw mut` operators, by assignment from a reference, or by an explicit function/cast. Creating a raw pointer is considered safe, since they do not have to be valid. However, dereferencing a raw pointer is `unsafe`, meaning that it is the programmer's responsibility to ensure that it points to a valid object before dereferencing. Raw pointers also have a notion of "provenance", which tracks their origin (think capabilities in the CHERI sense here; indeed CHERI was the motivation for the "strict provenance" APIs: https://doc.rust-lang.org/std/ptr/index.html#strict-provenance). >For Haskell I can at least make types explicit at various places to >reduce the area where the error might hide, but in Rust I have no way >to disable the automagic, and no way (or at least I don't know it) to >tell the borrow checker what I think it should be seeing, such that it >can tell me where my error is. This is classic, "fighting the borrow checker." The checker will tell you exactly where it thinks your error is, as far as saying, "you use a reference at this point, when the object it points to went out of scope at this other point..." or "you attempted to take a mutable reference to an object while another live reference to it already existed..." or "you tried to take *any* reference to an object while a live mutable reference exists". There's no magic here, and generally the error messages are pretty good. The `unsafe` keyword is the escape hatch that tells the compiler that I, the programmer, am doing something weird that it has no insight into, and so it cannot automatically enforce its checks, and that I am therefore taking on the responsibility of ensuring that the language's rules with respect to memory safety and concurrency are respected. Note that `unsafe` does not allow a programmer to _violate_ those rules, it merely tells the compiler that they've been checked manually. Once you get used to it, the borrow checker is unintrusive, and it really does catch errors that would lead to crashes or mangled data in other languages. But if you are trying to write a Rust program the same way you would in a language that is not type- and memory-safe, like C or (I gather) Forth, you will almost certainly find it very frustrating. >I have investigated some tool that IIRC rewrite the Rust program to >give more information about how it understands the program, but at >least for my test case, it was not enlightening. Perhaps you are referring to Miri? That is a simulator for the IR, that can be used to find bugs in raw pointer usage in `unsafe` code. >As for the type system, a friend told me that he likes Rust because it >gives him what he likes about Haskell's type system in an imperative >language. Yes. The type system allows you to make aggressive use of things like "typestates" and "newtypes" to eliminate entire categories of errors. The "Parse, don't validate" metaphor introduced by Alexis King on her blog post (https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/) translates almost directly from Haskell to Rust; my colleague Cliff Biffle has written on typestates in Rust here: https://cliffle.com/blog/rust-typestate/ When I wrote the host OS boot loader for our machines (the thing we run from the reset vector) I used newtypes to describe page mappings so that virtaul page addresses of a certain size _had_ to be canonical and properly aligned and _had_ to point to a correctly sized and aligned physical page frame that was similarly valid. Once one has parsed a virtual page address or physical frame address into its representative type, there's no need to revalidate it at point of use. The existence of an instance of those types is a proof that the addresses are valid: https://github.com/oxidecomputer/phbl/blob/main/src/mmu.rs These techniques apply to all sorts of things. Making a erroneous combination unrepresentable at the type level is an incredibly powerful tool. - Dan C.
Technical Details
Message-ID:
<11a5kkp$i58$1@reader2.panix.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mailReferences:
<1177fdi$26ea4$1@dont-email.me> <2026Oct3.131214@mips.complang.tuwien.ac.at> <861pa3m40z.fsf@linuxsc.com> <2026Oct6.172109@mips.complang.tuwien.ac.at>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail From: cross@spitfire.i.gajendra.net (Dan Cross) Newsgroups: comp.arch Subject: Re: Rust (was: Optimization by programmers) Date: Wed, 7 Oct 2026 14:23:21 -0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: <11a5kkp$i58$1@reader2.panix.com> References: <1177fdi$26ea4$1@dont-email.me> <2026Oct3.131214@mips.complang.tuwien.ac.at> <861pa3m40z.fsf@linuxsc.com> <2026Oct6.172109@mips.complang.tuwien.ac.at> Injection-Date: Wed, 7 Oct 2026 14:23:21 -0000 (UTC) Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80"; logging-data="18600"; mail-complaints-to="abuse@panix.com" X-Newsreader: trn 4.0-test77 (Sep 1, 2010) Originator: cross@spitfire.i.gajendra.net (Dan Cross)