Skip to content

Instantly share code, notes, and snippets.

@46bit
Created December 12, 2014 15:00
Show Gist options
  • Save 46bit/f96bac392c8bb21a8e46 to your computer and use it in GitHub Desktop.
Save 46bit/f96bac392c8bb21a8e46 to your computer and use it in GitHub Desktop.
What pointers do I need to add to make this work? Edges should store references to nodes; nodes store references to edges.
struct Node<D> {
edges: Vec<Edge<D>>,
pub data: D
}
impl<D> Node<D> {
pub fn new(data: D) -> Node<D> {
Node{data: data, edges: Vec::new()}
}
}
struct Edge<D> {
nodes: [Node<D>, .. 2],
pub cost: f64
}
impl<D> Edge<D> {
pub fn new(node_a: & mut Node<D>, node_b: & mut Node<D>, cost: f64) -> Edge<D> {
let new_edge = Edge{nodes: [node_a, node_b], cost: cost};
node_a.edges.push(new_edge);
node_b.edges.push(new_edge);
new_edge
}
}
struct NodeInfo {
x: u32,
y: u32,
symbol: char,
symbol_cost: f32
}
fn main() {
let mut a = Node::new("Alpha");
let mut b = Node::new("Beta");
let mut c = Node::new("Gamma");
println!("{}, {}, {}", a.data, b.data, c.data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment