this post was submitted on 16 May 2024
5 points (100.0% liked)

Learning Rust and Lemmy

391 readers
1 users here now

Welcome

A collaborative space for people to work together on learning Rust, learning about the Lemmy code base, discussing whatever confusions or difficulties we're having in these endeavours, and solving problems, including, hopefully, some contributions back to the Lemmy code base.

Rules TL;DR: Be nice, constructive, and focus on learning and working together on understanding Rust and Lemmy.


Running Projects


Policies and Purposes

  1. This is a place to learn and work together.
  2. Questions and curiosity is welcome and encouraged.
  3. This isn't a technical support community. Those with technical knowledge and experienced aren't obliged to help, though such is very welcome. This is closer to a library of study groups than stackoverflow. Though, forming a repository of useful information would be a good side effect.
  4. This isn't an issue tracker for Lemmy (or Rust) or a place for suggestions. Instead, it's where the nature of an issue, what possible solutions might exist and how they could be or were implemented can be discussed, or, where the means by which a particular suggestion could be implemented is discussed.

See also:

Rules

  1. Lemmy.ml rule 2 applies strongly: "Be respectful, even when disagreeing. Everyone should feel welcome" (see Dessalines's post). This is a constructive space.
  2. Don't demean, intimidate or do anything that isn't constructive and encouraging to anyone trying to learn or understand. People should feel free to ask questions, be curious, and fill their gaps knowledge and understanding.
  3. Posts and comments should be (more or less) within scope (on which see Policies and Purposes above).
  4. See the Lemmy Code of Conduct
  5. Where applicable, rules should be interpreted in light of the Policies and Purposes.

Relevant links and Related Communities


Thumbnail and banner generated by ChatGPT.

founded 9 months ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] Jayjader@jlai.lu 2 points 6 months ago (1 children)

[...] Rust’s rules around lifetimes [though] usually framed in the public consciousness as “a way to avoid garbage collection,” [...] are a much deeper and more significant construct than that. They are [...] an incredibly powerful tool for understanding the behavior of the system because you can analyze [it] locally: you never need to worry about “spooky action at a distance.” And you can do so without manually reconstructing imperative control flow with monads. As I’ve written several times before: monads are a clever way to show you can program without mutation; lifetimes are an even cleverer way to show you can just use mutation.

This is the single "deepest" lesson that I feel like I've learned about Rust so far. Lifetimes, stumbled upon while trying to avoid (runtime) garbage collection, are in fact a more general tool for delimiting behavior/causality in programs.

I’m not saying that Rust’s solution is perfect. Indeed, lifetime syntax is arcane and confusing to users. But I think any solution that claims to beat Rust should provide the same level of guarantee or indeed better: ideally new languages would allow for imperative programming without any shared mutable state at all.

Similarly, this aligns with my current opinion of rust vs other programming languages for tasks that are not inherently unsuited to Rust's current state.

I'm not sold on the idea of "imperative programming without any shared mutable state at all". Maybe I just can't accurately imagine what that would look like in practice.

[–] maegul@lemmy.ml 2 points 6 months ago* (last edited 6 months ago) (1 children)

I’m not sold on the idea of “imperative programming without any shared mutable state at all”. Maybe I just can’t accurately imagine what that would look like in practice.

Neither.

for tasks that are not inherently unsuited to Rust’s current state

Curious where you draw the line on this.

This is the single “deepest” lesson that I feel like I’ve learned about Rust so far. Lifetimes, stumbled upon while trying to avoid (runtime) garbage collection, are in fact a more general tool for delimiting behavior/causality in programs.

Interesting you use the word "causality" there ... do you have anything in particular in mind or are you generally thinking about trying to nail down what the cause of a bug may be? I liked the phrase from the article (which you quote): you never need to worry about “spooky action at a distance.” (emphasis mine).

[–] Jayjader@jlai.lu 2 points 6 months ago* (last edited 6 months ago) (1 children)

unsuited to Rust's current state

for me this mostly comes down to the state of the ecosystem (game dev with godot, websites in js are 2 cases where I'm not really interested in using rust) but for example when I want to just solve a problem and the input size won't cause a naive approach to run very slow, I'll probably grab python instead or if the solution is something hyper dynamic (a la dynamic programming), it would probably be simpler in ocaml or haskell or f# - ie something more "truly" immutable so that you don't have to worry about data lifetimes or manual memory management (and it's generally very cheap and elegant to push & pop from stacks and queues in such languages)

re: causality and "spooky action at a difference"

It's more:

  1. mutable vs immutable references mean any mutable access must happen before or after all other accesses [to the same data]

  2. any access to a moved value must happen before the move itself

So ownership effectively imposes a certain causal order on your code, that emerges from which behavior uses/accesses which data. In many aspects it's way too hazy to act as a "proper" causal order, but I often find it easier to write code that passes the borrow checker on the first try when I try to organize my code according to this notion.

I would almost describe my mental image as if there were compiler-enforced RWLocks on all mutable references, and as if all moves are somehow telling the os to change which virtual memory address is mapped onto the (moved) data (without actually copying the data into a different part of memory).

[–] maegul@lemmy.ml 1 points 6 months ago

Yea nice.

I'd kinda developed the same attitude but never really thought about it in terms of "causality" as you put it. Instead I had more of a "clean code" or "efficiency" framing, but I like yours much better.