In the following code, I am trying to change the value of a refcounted object by calling one of its methods:
use std::rc::Rc;
fn main() {
let mut x = Rc::new(Thing { num: 50 });
x.what_to_do_to_get_mut_thing().change_num(19); //what do i do here
}
pub struct Thing {
pub num: u32,
}
impl Thing {
pub fn change_num(&mut self, newnum: u32) {
self.num = newnum;
}
}
I am using the get_mut
function to achieve this, but I don't know if this is a standard way to accomplish this.
if let Some(val) = Rc::get_mut(&mut x) {
val.change_num(19);
}