Skip to content

Instantly share code, notes, and snippets.

@Tristramg
Last active August 29, 2015 13:57
Show Gist options
  • Save Tristramg/9357067 to your computer and use it in GitHub Desktop.
Save Tristramg/9357067 to your computer and use it in GitHub Desktop.
// This is the initial idea: a graph structure that is a hash containing a vector of target node and edge properties
// Doesn’t compile with
// src/graph.rs:56:32: 56:37 error: cannot move out of dereference of `&`-pointer
// src/graph.rs:56 Some(list) => for &(n,e) in list.iter() {
trait Graph<Node, Edge> {
fn children(&self, &Node, |Node, Edge| -> ()); // iterate on outgoing edges
}
impl<N:Hash+Eq,E> Graph<N,E> for HashMap<N, ~[(N,E)]> {
fn children(&self, out_node: &N, f : |N, E| -> ()) {
match self.find(out_node) {
None => (),
Some(list) => for &(n,e) in list.iter() {
// Some(list) => for n in list.iter() {
// works, but how could I directly match on the tuple?
println!("{:?}, {:?}", n, e);
// f(n,e);
}
};
}
}
// Just to play arround I tried the following that worked as I expected it
extern crate collections;
use collections::HashMap;
fn main() {
let mut g = HashMap::new();
let v = ~[(1,2), (3,4)];
g.insert(~"foo", v);
match g.find(&~"foo") {
None => println!("Missing"),
Some(vec) => for &(a,b) in vec.iter() {
println!("{:?}, {:?}", a, b);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment