NLL and RefCell

This sounds like a long existing issue with borrows in RefCell and the likes. Not a 100% sure, but I've experienced something similar here. As a correction this works:

#![feature(nll)]
use std::cell::RefCell;

struct Foo {
    pub v: Vec<u32>,
    pub b: u32,
}

fn f(foo: RefCell<Foo>) {
    let mut foo = foo.borrow_mut();
    let b = foo.b;
    foo.v.push(b);
    
}

fn main() {
    let foo: RefCell<Foo> = RefCell::new(Foo {v: Vec::new(), b: 0});
    f(foo);
}

PS: I think essentially the compiler first borrows mutably foo for the push and hence does not allow to reference any of its internals until the borrow is done. Similar to the issue I've pointed out these are compiler issues and depend on how the AST is parsed and borrows evaluated.

1 Like