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
}
}
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?)
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.
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).
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.
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.