I'd like to ask you about the following.
We used Rc
and RefCell
to verify the operation as follows.
use std::borough::BorrowMut;
usestd::cell::RefCell;
use std::rc::Rc;
# [derive (Debug)]
US>structure Test {
num —i32,
}
US>fnmain(){
leta=Rc::new(RefCell::new(Test {num:100});
let a1 = a.clone();
a.borough_mut().num=100;
a1.borough_mut().num = 100;
println!("a:{:?}", a);
println!("a1:{:?}", a1);
}
Then, the following error occurred while substituting 100.
error [E0609]: no field `num` on type`&mut Rc<RefCell<Test>>`
-->src/main.rs:13:20
|
13 | a.borough_mut().num = 100;
| ^^^unknown field
I was looking at the operation with reference to the following, but I am wondering if there is any error like this.
How can I modify the contents of the Test
structure?
Thank you for your cooperation.
trust
error [E0609]: no field `num` on type`&mut Rc<RefCell<Test>>`
If you look at the error message, it seems that RefCell::borough_mut and then you want to access .num, but unlike your intentions, Rc's BorrowMut::::borough_mut is called.
If you do not use the BorrowMut trace, Rc does not have the Borrow_mut method, which causes a de-reference (de-ref) to occur, and the Borrow_mut of the inner RefCell is called.
https://play.rust-lang.org/?version=stable&mode=debug&edition=8326a0&gcf=8327b8327
© 2024 OneMinuteCode. All rights reserved.