Questions tagged [interior-mutability]

In Rust, interior mutability refers to cell type wrappers, such as Cell and RefCell, which allow you to make data mutable, without needing to pass mutable references around.

In Rust, interior mutability refers to cell type wrappers, such as Cell and RefCell, which allow you to make data mutable, without needing to pass mutable references around.

Interior mutability provides an escape hatch for when it would be inconvenient or impossible to make use of mutable references. While access to mutable references is checked at compile-time, controlling access to data wrapped in a cell is deferred until runtime.

38 questions
56
votes
5 answers

How do I return a reference to something inside a RefCell without breaking encapsulation?

I have a struct that has inner mutability. use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec, } struct Foo { //although not used in this particular snippet, //the motivating problem uses interior…
Drew
  • 8,675
  • 6
  • 43
  • 41
45
votes
1 answer

What is the difference between Rc> and RefCell>?

The Rust documentation covers Rc> pretty extensively but doesn't go into RefCell>, which I am now encountering. Do these effectively give the same result? Is there an important difference between them?
Gman man
  • 475
  • 1
  • 4
  • 8
37
votes
3 answers

Situations where Cell or RefCell is the best choice

When would you be required to use Cell or RefCell? It seems like there are many other type choices that would be suitable in place of these, and the documentation warns that using RefCell is a bit of a "last resort". Is using these types a "code…
jocull
  • 20,008
  • 22
  • 105
  • 149
21
votes
1 answer

How to access value in RefCell properly

I'm trying to wrap my head around Rc and RefCell in Rust. What I'm trying to achieve is to to have multiple mutable references to the same objects. I came up with this dummy code: use std::rc::Rc; use std::cell::RefCell; struct Person { name:…
Christoph
  • 26,519
  • 28
  • 95
  • 133
16
votes
1 answer

How do I borrow a RefCell, find a key, and return a reference to the result?

I have a RefCell and want to borrow the table, find a key, and return a reference to the result: use std::cell::RefCell; use std::collections::HashMap; struct Frame { map: RefCell>, } impl Frame { fn new()…
Marc Miller
  • 161
  • 1
  • 6
14
votes
3 answers

Is there a way to make an immutable reference mutable?

I want to solve a leetcode question in Rust (Remove Nth Node From End of List). My solution uses two pointers to find the Node to remove: #[derive(PartialEq, Eq, Debug)] pub struct ListNode { pub val: i32, pub next:…
Aylei
  • 368
  • 2
  • 8
12
votes
1 answer

Interior mutability abuse in API design?

My background in C++ makes me uncomfortable about interior mutability. The code below is my investigation around this topic. I agree that, from the borrow checker point of view, dealing with many references on every single struct which internal…
prog-fh
  • 13,492
  • 1
  • 15
  • 30
11
votes
2 answers

A cell with interior mutability allowing arbitrary mutation actions

Standard Cell struct provides interior mutability but allows only a few mutation methods such as set(), swap() and replace(). All of these methods change the whole content of the Cell. However, sometimes more specific manipulations are needed, for…
user396672
  • 3,106
  • 1
  • 21
  • 31
7
votes
1 answer

Is there an alternative or way to have Rc> that restricts mutability of X?

For example given this code: use std::rc::Rc; use std::cell::RefCell; // Don't want to copy for performance reasons struct LibraryData { // Fields ... } // Creates and mutates data field in methods struct LibraryStruct { // Only…
Jake
  • 245
  • 2
  • 10
6
votes
1 answer

Why Mutex was designed to need an Arc in Rust

Why was Mutex designed to need an Arc if the only reason to use a Mutex is for concurrent code, i.e. multiple threads? Wouldn't it be better to alias a Mutex to an atomic reference in the first place? I'm using…
Lev
  • 1,698
  • 3
  • 18
  • 26
6
votes
1 answer

Difference between borrow_mut on a RefCell and RefCell<&X>

If I get correctly it is not possible to create a mutable borrow over a std::rc::Rc in Rust, you have to use Cell or RefCell. But anyway I cannot understand how to use them. For example consider this simple example: use std::cell::RefCell; struct X…
Kill KRT
  • 1,101
  • 2
  • 17
  • 37
5
votes
2 answers

Data to be determined later: interior mutability or separate HashMap?

I have a struct, call it Book, which let's say stores data on a book sold by a bookstore. It needs to be referenced at many places in some data structure (e.g. with Rc) and so cannot be borrowed mutably in the normal way. However, it has some…
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
5
votes
1 answer

Comparing value enclosed in RefCell

I have a structure with a field defined as follows: log_str: RefCell I performed various calls to borrow_mut() to call push_str(.) on the field. At the end, I'm assessing its value using: assert_eq!(os.log_str.borrow(), "
Jämes
  • 6,945
  • 4
  • 40
  • 56
5
votes
1 answer

How do I return an iterator that has a reference to something inside a RefCell?

I'm trying to create a method that returns an iterator over the values of HashMap that is boxed inside a RefCell, but i'm having an error where Ref returned by RefCell::borrow doesn't live long enough for iterator to be returned from the method.…
Marik
  • 97
  • 6
4
votes
1 answer

Why is my zero-cost alternative to RefCell not the standard way of achieving interior mutability?

I've been thinking about why interior mutability in Rust in most cases requires runtime checks (e.g. RefCell). It looks like I've found a safe alternative without a runtime cost. I've called the type SafeCell (mainly because it is a safe wrapper…
kreo
  • 2,613
  • 2
  • 19
  • 31
1
2 3