This is supplementary/separate from the Twitch Streams (see sidebar for links), intended for discussion here on lemmy.

The idea being, now that both twitch streams have read Chapter 4, we can have a discussion here and those from the twitch streams can have a retrospective or re-cap on the topic.

This will be a regular occurrence for each discrete set of topics coming out of The Book as the twitch streams cover them


Ownership and the borrow checker are obviously a fundamental and unique topic to rust, so it’s well worth getting a good grounding in AFAICT.

  1. Anyone up to trying to summarise or explain ownership/borrow-checker in rust?
    • it can be a good exercise to test your understanding and get feedback/clarification from others … as well as probably a good way to teach others
  2. Any persistent gripes, difficulties or confusions?
  3. Any of the quizzes from The Book stump you?
  4. Any hard learnt lessons? Or tried and true tips?
  • maegul@lemmy.mlOPM
    link
    fedilink
    English
    arrow-up
    1
    arrow-down
    1
    ·
    edit-2
    1 month ago

    4. Any hard learnt lessons? Or tried and true tips?

    A basic lesson or tip from a discussion in this community (link here):

    PS: Abso-fucking-lutely just clone and don’t feel bad about it. Cloning is fine if you’re not doing it in a hot loop or something. It’s not a big deal. The only thing you need to consider is whether cloning is correct - i.e. is it okay for the original and the clone to diverge in the future and not be equal any more? Is it okay for there to be two of this value? If yes, then it’s fine.

    IE, using copy/clone as an escape hatch for ownership issues is perfectly fine.


    Another one that helps put ownership into perspective I think is this section in the Rustonomicon on unsafe rust, and the section that follows:

    There are two kinds of reference:

    • Shared reference: &
    • Mutable reference: &mut

    Which obey the following rules:

    • A reference cannot outlive its referent
    • A mutable reference cannot be aliased

    That’s it. That’s the whole model references follow.

    Of course, we should probably define what aliased means.

    error[E0425]: cannot find value `aliased` in this scope
     --> <rust.rs>:2:20
      |
    2 |     println!("{}", aliased);
      |                    ^^^^^^^ not found in this scope
    
    error: aborting due to previous error
    

    Unfortunately, Rust hasn’t actually defined its aliasing model. 🙀

    While we wait for the Rust devs to specify the semantics of their language, let’s use the next section to discuss what aliasing is in general, and why it matters.


    Basically it highlights that rust’s inferential understanding of the lifetimes of variables is a bit coarse (and maybe a work in progress?) … so when the compiler raises an error about ownership, it’s being cautious (as The Book stresses, unsafe code may not have any undefined behaviour).

    It helps I think reframe the whole thing as not being exclusively about correctness but just making sure memory bugs don’t happen


    Last lesson I think I’ve gained after chapter 4 was that the implementation and details of any particular method or object matter. The quiz in chapter 6 (question 5) I’ve mentioned is I think a good example of this. What exactly the Copy and Clone trait are all about too … where I found looking into those made me comfortable with the general problem space I was navigating in working with ownership in rust. Obviously the compiler is the safe guard, but you don’t always want to get beaten over with ownership problems.