Calling method of Some in a borrowed Option without moving it

Hey,

I have a struct X contained in a Vec<Rc<RefCell>. X contains an Option where Y implements the method foo(). Now I want to call foo() but I get

"cannot move out of borrowed content"

I call it with

vector[0].borrow_mut().y.unwrap().foo()

where vector is of the type Vec<Rc<RefCell> and y is Option. I know that unwrap() is the forbidden move but I don't know how to call foo() otherwise.

You can call the as_mut function to first turn the borrowed option into an option of a borrow:

vector[0].borrow_mut().y.as_mut().unwrap().foo()
2 Likes

Great, it works, thank you :slight_smile: After one year of using Rust occasionally the borrowing system is still sometimes a little bit confusing to me.

1 Like