RefCell<Vec<T>> vs Vec<RefCell<T>> for dynamic programming / memoization

  1. The code is single threaded.
  2. I do not expect to run into any double borrow_mut() issues.

I'm doing some dynamic programming / memorization which requires building a cache of sorts. I'm 99% convinced, given conditions 1 & 2 above, that RefCell<Vec<T>> is strictly superior to Vec<RefCell<T>> (because we only pay the memory cost of one RefCell). However, this is my first time doing memorization in Rust, so if I'm wrong / unaware of reasons to use Vec<RefCell<T>>, I'm interested in hearing them as well.

Unless RefCell<Vec<T>> results in borrow_mut() errors you are not able to resolve, it is strictly superior to Vec<RefCell<T>>.

2 Likes