8 ms·
The Registers of Rust
- zabzonk 4y agoi really don't like the "f"s in that font in the section titles. every time i came across one, it brought my reading to a juddering stop.
- dang 4y agoI completely understand the feeling but the site guidelines ask us not to convert such feelings into HN posts: "Please don't complain about tangential annoyances—e.g. article or website formats, name collisions, or back-button breakage. They're too common to be interesting." https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- withoutboats3 4y agoI like the f's a lot actually but I think there's a clear problem with the spacing when a title is two lines, and the f's really exacerbate it. But I also don't want to fiddle with my CSS.
- Narishma 4y agoUse the reader mode of your browser.
- stathibus 4y ago[flagged]
- sockaddr 4y agoNo. But I’ll readily admit to upvoting stuff like this submission.
- steveklabnik 4y agoHacker News attempts to prevent bot upvotes, last I heard. So even if there were, it would not work, and if it did, I'm sure the hacker news team would work towards fixing that. This is simply a high-quality, very technical blog post from someone who's had a tremendous impact on a popular project. Hence upvotes.
- Dowwie 4y agoIt's an army of Rust evangelists. We have a lot of time on our hands waiting for compiles.
- wyldfire 4y ago> Will I use references and borrowing, or will I liberally use clone to avoid it? I haven't written much Rust but when I did I was very liberal with calls to .clone().
- steveklabnik 4y agoThat is, in my opinion, the way it should be.
- wyldfire 4y agoReally? I felt like I was just punting because I didn't take time to understand and resolve potential dependencies. In my case the data was relatively small and I just wanted functionality. But I felt like if I could have worked out borrowing it would've generated more efficient code and less memory consumption.
- lordnacho 4y agoI'm undecided. I feel it's a bit like when you .unwrap() everything. Either I take the time to match all the potential results, or I do it quick and dirty. But also when it comes to cloning if you don't do it you get a lot of lifetime annotations.
- steveklabnik 4y agoWe put this aside in the book for this exact reason: https://doc.rust-lang.org/stable/book/ch12-03-improving-error-handling-and-modularity.html?#the-trade-offs-of-using-clone https://doc.rust-lang.org/stable/book/ch12-03-improving-erro... > But I felt like if I could have worked out borrowing it would've generated more efficient code and less memory consumption. That is possible! But while you're learning, this isn't the most important thing to focus on. And depending on what you're trying to do, using borrowing may be easy, or may be hard, and while you're trying to learn, you don't know enough to evaluate the tradeoff on that basis, so in my humble opinion, it is absolutely okay to clone, and is probably even what you should do in most cases. Once you're feeling good enough about Rust to start digging deeper, working on trying to not do this is a valuable skill. But imho it's a waste of time when you're starting, and may lead you into a pit of despair.
- phab 4y agoI found this article enlightening; an example of where a little formalism/structure (in the notion of the four registers) really helps identify the missing pieces in the wider picture. I did find the use of the term "register" slightly jarring given its somewhat overloaded nature between its natural language meaning, used here, and also the sense of "a specific place in which to store a datum". Reading the Wikipedia page on the term, I came across "diatype", which to me seemed to convey the same meaning without the potential confusion from the polysemantics of "register"...?
- steveklabnik 4y ago"register" is often used in the sense boats uses it in like a... let's say English graduate student sense? Given that "withoutboats" is a Foucault reference, this shouldn't surprise anyone who's familiar with that intellectual tradition. For a bit more discussion on this, there's a fantastic comment on the Reddit discussion https://www.reddit.com/r/rust/comments/11m0j30/comment/jbflzdt/ https://www.reddit.com/r/rust/comments/11m0j30/comment/jbflz...
- iviv 4y agoNow I’m interested in the Foucault reference :)
- steveklabnik 4y agoIt's right there in the header, "Without boats, dreams dry up." The full quote is "In civilizations without boats, dreams dry up, espionage takes the place of adventure and the police take the place of pirates," from his work "Of Other Spaces, Heterotopias."
- PaulDavisThe1st 4y ago"Dialect" seems more appropriate me.
- withoutboats3 4y ago
- satvikpendem 4y agoThe author mentions a pattern emerging, and I do too, which, as he mentions, motivates keyword generics, but perhaps we should go all the way and have full algebraic effects in Rust? I know OCaml 5 has them, one of the first, so perhaps Rust might adopt something similar. Then again, I'm not sure how it'd work with the borrow checker.
- zozbot234 4y ago> full algebraic effects in Rust? Isn't that what the "keyword generics" initiative is all about? The Rust dev team seems to be quite aware that algebraic effects in Rust might be desirable, but coming up with a good design might involve quite a bit of work. It took a long time for even the async MVP to shape up properly, and supporting full effects might be even more complicated.
- satvikpendem 4y agoNot exactly [0]. The keyword generics initiative is constrained, it is not a full effects system. It is akin to GATs versus HKTs, a limited form of a more full feature, but I still think the full versions would make the API cleaner than the more limited implementations now. Again, this is with me not knowing how it'd work with the borrow checker, but like saurik mentioned above, I think there is a way to have a Rust-like language that could embody all of these full-feature aspects cleanly. [0] https://blog.rust-lang.org/inside-rust/2022/07/27/keyword-generics.html#q-are-you-building-an-effect-system https://blog.rust-lang.org/inside-rust/2022/07/27/keyword-ge...
- zozbot234 4y agoThe blogpost just says that they're trying to minimize surface area and not expose things like e.g. user-defined effects. That's normal for a MVP where we don't even know how such custom effects might interact with the rest of the language. If it turns out that they can be modeled "cleanly" as you say, they can be added quite easily in the future.
- tel 4y agoI'm very curious about this statement, does anyone know more detail > Futures combinators are not used because the combinator model suffered some specific problems with state sharing in futures
- withoutboats3 4y agoIt's exactly the problem that led us to Pin in designing async/await. Futures need to own all of their state. If you want to use that state across multiple combinators (in async/await terms, "across an await point"), given that rust doesn't have GC, you have to somehow make it available to both closures. This meant Arc<Mutex<_>> around things even though they were being used in sequence, because both closures needed to own the state and the compiler doesn't know the closures will only be called in sequence. It was a mess, and was a big hurdle for adoption of futures before async/await was added.
- tel 4y agoThat makes sense, thank you. I had been a little confused as I'd seen the combinators available in the futures crate (along with Stream) and it's a little tricky to figure out the status and review of techniques that are implemented there but not standardized.
- atq2119 4y agoAll those effects have the same issue, don't they? I've definitely run into situations where I moved from a combinational approach to a control flow based one for iteration because the combinational approach required multiple closures to borrow the same state mutably. It's almost like there's also still some gap in the borrow checking, but it's not obvious to me how to address that.
- munificent 4y agoThis is an excellent, very insightful post. When Erik Meijer worked with Gilad Bracha and others on the Dart team to bring async to Dart, he approached with a very similar "lets look at the table of all the combinations and make sure we didn't leave any holes". In this case, the two axes (effects) he considered were asynchrony and iteration. So Dart supports all four combinations: * Synchronous single-value functions are just normal function declarations. Within one, you use `return` to yield the one value. * Synchronous multi-value functions are generators, marked `sync`, which return a `Iterable<T>`. Within one, you use `yield` and `yield` to emit values. * Asynchronous single-value functions are marked `async` and return a `Future<T>`. Within on, you use `await` to pause on asynchronous operations and consume values. A `return` valued is implicitly wrapped in a future. * Asynchronous multi-value functions are marked `async` and return a `Stream<T>`. Within one, you can use both `yield` and `yield` to emit values and `await` to suspend and consume asynchronous values. We also have an asynchronous `await for` statement that can be used to imperatively consume a stream. There's no third axis for fallibility, because Dart chooses to comprehensively use exceptions for operations that fail (which I quite like). Overall, it's a really rich, comprehensive approach to the problem. I have mixed feelings about it. It really does cover all the bases. But, in practice, `async` functions and `await for` are very* rarely used but add a lot of complexity to the language and engineering burden to the implementations.
- couchand 4y ago> There's no third axis for fallibility, because Dart chooses to comprehensively use exceptions for operations that fail (which I quite like). Can you catch exceptions? Can you catch them when thrown iterating an Iterable, awaiting a Future, or iteratively awaiting values from a Stream? It doesn't seem like you can get around needing fallibility. What if I have an asynchronous operation that can fail? What if I need a sequence of possibly-empty values?
- klodolph 4y ago> It doesn't seem like you can get around needing fallibility. There’s no axis for fallability because all operations are fallible. Yes, you can catch exceptions when iterating, awaiting, or iteratively awaiting. You have fallibility. What you don’t have is a way of marking operations as non-fallible.
- ezekiel68 4y agoI'm still only really a rust dabbler, but I was very happy to see your blog, boats. I always found your writings, talks, and online meeting contributions to be very thoughtful when you were more involved. I hope the current leadership team will reach out to you from time to time, as you offered at the end of this piece.
- harikb 4y agoThe article talks about options (english meaning) as in the developer is choosing to model his program to use a combination of these registers. Unfortunately, that is exactly my problem. __There should be one-- and preferably only one --obvious way to do it.__
- steveklabnik 4y agoThat is a useful design maxim, but the problem is that in a "systems language," it is difficult if not impossible, in my opinion, to achieve. The closer you get to physical reality, the harder it is to be able to fit the world into your model, and you need to fit your model to the world. In Rust, unsafe is the way to manage this difficult set of tradeoffs
- jenadine 4y agoThat mean even more colours for our functions?
- saurik 4y agoI will claim that the most important thing to understand about all three of the concepts in this article -- Fallibility, Asynchrony, and Iteration (as well as some that aren't discussed, but are to-me relevant, such as "Allocation: holds scoped resources" <- one I feel Rust and C++ reasonably-correctly assume every object / value might one day take part in) -- is that these are all monads (yes: including Iteration/List, which I think took me the longest to fully appreciate from the set of commonly-referenced monads); and, in a language with a more theoretically-sound core (aka, Haskell... and no: I am not a Haskell zealot by any means and in fact have wasted away most of my life for decades using C++ and Python), these "control-flow effects" (which is frankly a better name than "monad") are all supported using the same typeclass and rely on the same syntax, which not only ensures that every such "register" is correctly analogous with the others but even allows for end developers to extend the set of similar primitives without resorting to increasingly-fraught language modifications.
- c54 4y agoI think you’re making a good point that I probably agree with, but I can’t tell partially because your writing style is hard to follow. Your whole comment could be improved by separating into multiple sentences and moving the asides out of the main body of thought
- saurik 4y agoIt could certainly be improved for reading at a glance... but, if you just pay attention to the punctuation, I kind of think inlining the "asides" there is better than simply trying to tree them out (which would be the fast way to fix it). Like: this is a comment, not an article... I am lying in bed (deeply wishing I were asleep) trying to quickly type a train of thought into my phone (a smaller one than I normally even use) and am even feeling forced to do so under a time pressure (as once a post gets enough comments you won't ever find anything). Often, I will even type such a comment at first and then--after posting it--take the next 20 minutes to "clean it up" into some paragraphs (but am not going to do that this time as I am hurting my elbow). You are welcome to just ignore my comment for today; and, maybe one day, I will have more than a few minutes to allocate to writing yet-another-article-on-monads that no one will want to read (and likely won't help anyone understand... see "the monad tutorial fallacy", one of my favorite articles / concepts ;P).
- nazka 4y agoEvery time withoutboat drops something it’s crazy. I don’t know how people can know all that and have the intuition of it. Hard not to be a fanboy of the Rust community. :) Awesome post! I hope things like that will move forward.
- brundolf 4y agoThis helped congeal a thing I've been thinking about in my Rust usage for a long time, but didn't know how to express: > In Rust, this distinction in register can look like this: > - Will I use references and borrowing, or will I liberally use clone to avoid it? > In general, Rust strives to have an obvious register which to operate which is still going to be performant enough (and hopefully accessible enough!), while allowing users to switch register when they need to. One of the biggest ergonomics problems in Rust, in my experience, is that for certain things (like references vs clones, or generics vs enums vs dynamic dispatch), it can be very hard to change registers once you realize you need a different one. Often it means making changes throughout your entire codebase I don't have an easy solution but I feel like I at least have words for the problem now
- drewtato 4y agoThe strong typing in rust definitely helps with this, though. For most problems, you can encapsulate a refactor into something that, at each occurrence, either works automatically (with things like generics and deref) or fails to compile.
- zamubafoo 4y agoI think Rust is actually pretty good at this. If you do have to do a change that will affect your entire code base (or at least will be pervasive), the compiler will be able to tell you where things are wrong with a good degree of precision.
- brundolf 4y agoIt'll tell you, yes, but you'll still have to make non-automatable changes in a whole lot of different places in many situations. In situations where other languages might not require any changes at all
- vlthr 4y agoThis is exceptionally well communicated. Language design is really hard, and it's so easy to lose sight of the bigger picture when trying to find the next incremental step. This post helped me understand a lot of vague frustrations I've experienced in the past, and the bigger picture it outlines really resonates with me.