About visiting items of A_structure in Rc<RefCell<A_Structure>>

GreetingS! I'm a beginner, programming A* code to practice.Here's the thing. The Node i want can be pointed from many sub-nodes,so i use Rc<RefCell<>> to achieve multi-visiting and mutability. But, when i try to visit a item inside a Rc<RefCell>,using " . ", it failed. So,how can i visit without error? Or there might be a better way?
Code below i marked the error taken place

#[derive(Debug)]
pub struct Node<T> {
    f: T,                           //cost f=g+h
    g: T,                           //actual cost
    h: T,                           //predicted cost
    coord:(T,T),                //current node place
    parent: Option<Rc<RefCell<Node<T>>>>,   //parent node
}
impl<T> Node<T>
{
//new a node
    pub fn new(_f:T,_g:T,_h:T,_coord:(T,T),_parent:Option<Rc<RefCell<Node<T>>>>) ->Rc<RefCell<Node<T>>>{
        let pointer=Rc::new(RefCell::new(Node{
            f:_f,
            g:_g,
            h:_h,
            coord:_coord,
            parent:_parent 
        }));
        Rc::clone(&pointer)
    }
//cost func
    pub fn f(& self,end:Rc<RefCell<Node<T>>>)->T
        where T:AddAssign + std::ops::Add<Output = T>
    {
        g()
    }
//actual cost
    pub fn g(&self)->T
        where T:AddAssign+ std::ops::Add<Output = T>
    {
        let n=self.borrow_mut();
        let m=n.parent.unwrap();// m's type is Rc<RefCell<Node>>,i want item "g" inside m
        let parent_g=m.g;//////////////////////////HERE and BELOW 
        if n.coord.0-m.coord.0==0||n.coord.1-m.coord.1==0//when neighbor in vert/horiz line of current
        {
            parent_g+10
        }
        else//when neighbir in skew line
        {
            parent_g+14
        }
    }

You want RefCell::borrow(). Why not read the docs?

Sorry to bother again, it still seems not working.

        
        let m=n.parent.unwrap();// m is Rc<RefCell<Node<T>>>
        let parent_g=m.borrow();// IDE says parent_g is &i32, so i still cannot access "g" within  

I also found a similar case in Layout - Learning Rust With Entirely Too Many Linked Lists (rust-unofficial.github.io). The node of this web uses the same tpye as mine, it works with borrow() to access things within Rc<RefCell<Node>>. Though, don't konw why i failed

  1. It should be obvious that calling a method on a generic wrapper type (RefCell) that knows precisely nothing about your type cannot possibly directly give you a reference to a field that your type defines. Again, have you read the documentation? The borrow() method of RefCell simply gives you access to the inner value. If you want a field of the inner value, you will still have to extract it yourself. (Especially in the light of there being multiple fields, how can you expect that something magically guesses which one you wanted?)
  2. Don't believe the IDE. IDEs are wrong all the time. Use cargo on the command line and post the full compiler error. The truth will be there. IDEs often hide the real problem by trying too hard (and failing spectacularly) to parse the error message from the compiler. Don't use IDEs while you are a beginner, they only confuse you in the process of trying to be helpful by second guessing you.
  3. You have a bunch of other issues, eg.:
    • the self.borrow_mut() call on a &Node that's neither a mutable reference nor a RefCell itself;
    • the lack of self when calling g();
    • trying to move parent out of the n or self reference;
    • the variable n not even existing anywhere (it's supposed to be self from what I can tell);
    • the insufficient generic bounds on T;
    • trying to move out of m.g when m is a reference (judging by the names in the code, if this type is a simple integer, then this is fixable by requiring T to be Copy).
  4. There are some non-lethal, nevertheless problematic aspects:
    • The code formatting is ugly and very hard to read; you are not using either horizontal or vertical whitespace;
    • Cloning the Rc in new() is entirely useless.

Regardless, I was able to make your code compile, but I have zero idea whether it's actually doing what you want it to.

2 Likes

In the future it would help a lot if you could put your code that has the error into a playground so we can see the error. When I copy-paste your code and try to build it, I get different errors than what you've posted.

Make sure that building in the playground gives the error you want to discuss. You can share the playground by clicking Share and then clicking the link next to "Permalink to the playground" to copy the URL, which you can paste here.

If you can't build in the playground for some reason, then include complete code that can be built to show the problem. This includes the use imports.

1 Like

Much apprietiation for your critique, i misunderstood how to use RefCell and borrow(). I've got to read the doc again. thx

got it, sorry for the trouble

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.