Hello,
I'm currently learning Rust with Rust in Action, and my autocomplete engine automatically added the first commented line in my program which causes it to say that accessing the members of a Rc<RfCell< GroundStation >> is illegal, but if I comment it out everything works fine. I'm interested in why and also how the error occurs:
// use std::borrow::BorrowMut;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug)]
struct GroundStation {
radio_freq: f64, // Mhz
}
fn main() {
let base: Rc<RefCell<GroundStation>> =
Rc::new(RefCell::new(GroundStation { radio_freq: 87.65 }));
println!("base: {:?}", base);
{
let mut base_2 = base.borrow_mut();
base_2.radio_freq -= 12.34;
println!("base_2: {:?}", base_2);
}
println!("base: {:?}", base);
let mut base_3 = base.borrow_mut();
base_3.radio_freq += 43.21;
println!("base: {:?}", base);
println!("base_3: {:?}", base_3);
}