The Algebra of Loans in Rust

(nadrieril.github.io)

59 points | by g0xA52A2A 3 days ago

1 comments

  • the__alchemist 3 days ago
    I have what I thought was a broad knowledge base of rust an experience in it over many domains, but I haven't heard of most of those. Have been getting by with `&`, and `&mut` only from those tables!

    Incidentally, I think this is one of Rust's best features, and I sorely miss it in Python, JS and other languages. They keep me guessing whether a function will mutate the parent structure, or a local copy in those languages!

    Incidentally, I recently posted in another thread here how I just discovered the 'named loop/scope feature, and how I thought it was great, but took a while to discover. A reply was along the effect of "That's not new; it's a common feature". Maybe I don't really know rust, but a dialect of it...

    • goku12 1 hour ago
      I doubt that anybody truly knows Rust. And this is aggravated by the fact that features keep getting added. But here are two simple strategies that I found very effective in keeping us ahead of the curve.

      1. Always keep the language reference with you. It's absolutely not a replacement for a good introductory textbook. But it's an unusually effective resource for anybody who has crossed that milestone. It's very effective in spontaneously uncovering new language features and in refining your understanding of the language semantics.

      What we need to do with it is to refer it occasionally for even constructs that you're familiar with - for loops, for example. I wish that it was available as auto popups in code editors.

      2. Use clippy, the linter. I don't have much to add here. Your code will work without it. But for some reason, clippy is an impeccable tutor into idiomatic Rust coding. And you get the advantage of the fact that it stays in sync with the latest language features. So it's yet another way to keep yourself automatically updated with the language features.

      • VorpalWay 49 minutes ago
        I feel like other languages also have the issue of complexity and changing over time. I doubt I know all of C++ post C++14 for example (even though that is my day job). Keeping up with all the things they throw into the standard library of Python is also near impossible unless you write python every day.

        Rust has an unusually short release cycle, but each release tends to have fewer things in it. So that is probably about the same when it comes to new features per year in Python or C++.

        But sure, C moves slower (and is smaller to begin with). If that is what you want to compare against. But all the languages I work with on a daily basis (C++, Python and Rust) are sprawling.

        I don't have enough experience to speak about other languages in depth, but as I understand it Haskell for example has a lot of extensions. And the typescript/node ecosystem seems to move crazy fast and require a ton of different moving pieces to get anything done (especially when it comes to the build system with bundlers, minifiers and what not).

        • jacquesm 34 minutes ago
          Languages should be small, not large. I find that every language I've ever used that tries to throw everything and the kitchensink at you eventually deteriorates into a mess that spills over into the projects based on that language in terms of long term instability. You should be able to take a 10 year old codebase, compile it and run it. Backwards compatibility is an absolute non-negotiable for programming languages and if you disagree with that you are building toys, not production grade systems.
          • kreetx 20 minutes ago
            Haskell's user-facing language gets compiled down to Haskell "core" which is what the language actually can do. So any new language feature has a check in with sanity when that first transformation gets written.
          • aw1621107 21 minutes ago
            > Backwards compatibility is an absolute non-negotiable for programming languages

            What programming language(s) satisfy this criteria, if any?

      • gucci-on-fleek 26 minutes ago
        I don't know Rust at all, but all your comments

        > I doubt that anybody truly knows <language>.

        > Always keep the language reference with you.

        > Use <tool>, the linter.

        seem like they apply to all languages (and I agree that they're great advice!).

    • VorpalWay 1 hour ago
      Many of the things like "&own" are ideas being discussed, they don't exist in the language yet. As far as I know only &, &mut and raw pointers (mut and const) exist in stable rust at this point. The standard library has some additional things like NonNull, Rc, etc.
    • jibal 30 minutes ago
      > I sorely miss it in Python, JS and other languages. They keep me guessing whether a function will mutate the parent structure, or a local copy in those languages!

      Python at least is very clear about this ... everything, lists, class instances, dicts, tuples, strings, ints, floats ... are all passed by object reference. (Of course it's not relevant for tuples and scalars, which are immutable.)

    • GardenLetter27 1 hour ago
      Rust gives you no guarantees that a function won't allocate or panic though.
      • VorpalWay 42 minutes ago
        Yes that is annoying, but I don't know of any mainstream systems language that does. C and C++ can also have allocations anywhere, and C++ have exceptions. And those are really the only competitors to Rust for what I do (hard realtime embedded).

        Zig might be an option in the future, and it does give more control over allocations. I don't know what the exception story is there, and it isn't memory safe and doesn't have RAII so I'm not that interested myself at this point.

        I guess Ada could be an option too, but I don't know nearly enough about it to say much.

        • jibal 17 minutes ago
          Zig doesn't have exceptions, it has error unions, so basically functions return either a value or an error code and the caller is forced by the language to note which was returned. And instead of RAII it has defer ... which of course can easily be forgotten or mis-scoped, so it's not safe.
      • MaulingMonkey 1 hour ago
        This is something I do wish Rust could better support. A `#![no_std]` library crate can at least discourage allocation (although it can always `extern crate alloc;` in lib.rs or invoke malloc via FFI...)
        • maxbond 1 hour ago
          Is the juice worth the squeeze to introduce two new function colors? What would you do if you needed to call `unreachable!()`?

          It's a shame that you can't quite do this with a lint, because they can't recurse to check the definitions of functions you call. That would seem to me to be ideal, maintain it as an application-level discipline so as not to complicate the base language, but automate it.

          • MaulingMonkey 34 minutes ago
            > Is the juice worth the squeeze to introduce two new function colors?

            Typically no... which is another way of saying occasionally yes.

            > What would you do if you needed to call `unreachable!()`?

            Probably one of e.g.:

                unsafe { core::hint::unreachable_unchecked() }
                loop {}
            
            Which are of course the wrong habits to form! (More seriously: in the contexts where such no-panic colors become useful, it's because you need to not call `unreachable!()`.)

            > It's a shame that you can't quite do this with a lint, because they can't recurse to check the definitions of functions you call. That would seem to me to be ideal, maintain it as an application-level discipline so as not to complicate the base language, but automate it.

            Indeed. You can mark a crate e.g. #![deny(clippy::panic)] and isolate that way, but it's not quite the rock solid guarantees Rust typically spoils us with.

    • mring33621 3 days ago
      I'm just learning Rust but so far, it looks like the author is proposing some of these ref types, like &own and &uninit.

      I don't know 100% for sure. It's a bit confusing...

      • jojomodding 1 hour ago
        The part of the blog post where it says

        > What’s with all these new reference types? > All of these are speculative ideas

        makes it pretty clear to me that they are indeed not yet part of Rust but instead something people have been thinking about adding. The rest of the post discusses how these would work if they were implemented.

      • whytevuhuni 1 hour ago
        Right. The &pin, &own, and &uninit in the article (or rather everything except & and &mut in that table) do not exist in Rust.

        I have seen &pin being proposed recently [1], first time I'm seeing the others.

        [1] https://blog.rust-lang.org/2025/11/19/project-goals-update-o...

        • VorpalWay 46 minutes ago
          Own and uninit have been in discussions wrt in place construction. The Rust in the Linux kernel project seems to be the motivating use case for this that really got the effort going recently.
    • the8472 1 hour ago
      > All of these are speculative ideas, but at this point they’ve been circulating a bunch so should be pretty robust.