Discussion
Loading...

Post

  • About
  • Code of conduct
  • Privacy
  • Users
  • Instances
  • About Bonfire
laund
@laund@wetdry.world  ·  activity timestamp 2 days ago

@lambda @neilmadden I wouldn't ever describe what happens as a "anonymous storage cell" because it doesn't fit the way i think about Rust code, so let me try explain how i see it, maybe it helps:

if you do let s = 2; you have just the value 2. passing around s to functions etc happens by copy, since integers implement the Copy trait. If you instead do let s = &mut 2; you now have a reference that allowes mutation of 2. we sometimes call that a "mut(able) reference", which is a bit unclear as it could also mean the reference itself is mutable. But in fact its the value its referencing thats mutably borrowed. The let binding doesn't need to be mutable, as that would mean you want to modify its value, which is the reference. If you wanted to do s = <new val>, you'd need a let mut s. But you do *s = <new val> so you don't actually ever modify s, inly whats behind the reference. Most of the time, when i already have a &mut, where the value actually lives is only relevant on the exact line i take the &mut, since rusts borrow checker ensures exclusive access.

I can definitely understand how coming from C brings up these kinds of questions, but i find it neat i don't have to care that deeply.

It'll probably optimize to just let s = &mut 3; anyways.

  • Copy link
  • Flag this post
  • Block
Log in

bonfire.cafe

A space for Bonfire maintainers and contributors to communicate

bonfire.cafe: About · Code of conduct · Privacy · Users · Instances
Bonfire social · 1.0.0 no JS en
Automatic federation enabled
  • Explore
  • About
  • Members
  • Code of Conduct
Home
Login