Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think the easier way to think about Rust's interior mutability is via an example: (Cell is a mechanism for interior mutability)

  struct S {
     a: Cell<u64> 
  }

  struct T {
     b: u64 
  }
To mutate field `b` on `T`, we need a mutable object or reference. However, we can mutate field `a` on `S` via an immutable reference. Typically this is used to encapsulate some kind of interior state that isn't visible outside of the struct's methods, and helps in certain situations where using a mutable reference might cause some hair pulling due to the borrow checker


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: