I am implementing some structures to be used with search algorithms. These structures are to be stored in a Linked List during the execution of a program. After struggling a bit with integrating pointers in Rust I decided to do design the structures as it follows.
The following data structures was created to store the state:
pub struct Cell { map: Map, cost: CostType, player: Agent } The following data structure was created to store the information of a cell, its parent, and its child.
struct Node { state: *mut cell::Cell, parent: *mut cell::Cell, childs: [*mut cell::Cell; 4], } Implementing the constructors (new() methods) for this data structure is being troublesome. I know I am using raw pointers instead of the smart pointers, but it is the purpose for now. There is for sure something wrong with my syntax in the declaration of the method, but cannot find many references to sort this out. The implementation of the method for creating a new instance of a Node is the following:
fn new_node() -> Node{ Node{ state: &cell::Cell::new_cell() as *mut cell::Cell, parent: &cell::Cell::new_cell() as *mut cell::Cell, childs: [&cell::Cell::new_cell() as *mut cell::Cell, &cell::Cell::new_cell() as *mut cell::Cell, &cell::Cell::new_cell() as *mut cell::Cell, &cell::Cell::new_cell() as *mut cell::Cell] } } When I do this, the error that I obtain is the following one:
error[E0606]: casting `&node::cell::Cell` as `*mut node::cell::Cell` is invalid --> src/node/node.rs:15:20 | 15 | state: &cell::Cell::new_cell() as *mut cell::Cell, Using ptr::null_mut::cell::Cell() solves the issue, but I would like to know if there are any ways of solving this with the pointer operators.