Skip to content

Instantly share code, notes, and snippets.

@Tristramg
Created March 9, 2014 15:59
Show Gist options
  • Save Tristramg/9449905 to your computer and use it in GitHub Desktop.
Save Tristramg/9449905 to your computer and use it in GitHub Desktop.
trait GraphAlgorithm<Node, Edge> {
fn dfs_rec(&self, source: &Node, visitor: |&Node| -> (), visited: &mut HashSet<&Node>);
}
impl<'a, Node:Hash+Eq, Edge> GraphAlgorithm<Node, Edge> for &'a Graph<Node, Edge> {
fn dfs_rec<'a>(&self, source: &Node, visitor: |&Node| -> (), visited: &'a mut HashSet<&Node>) {
visitor(source);
self.children(source, |target,_| {
if !visited.contains(&target) {
// visited.insert(target);
// self.dfs_rec(target, visitor, visited);
};
});
}
}
src/graph.rs:40:38: 40:45 error: mismatched types: expected `&&Node` but found `&&Node` (lifetime mismatch)
src/graph.rs:40 if !visited.contains(&target) {
^~~~~~~
src/graph.rs:39:46: 44:14 note: an anonymous lifetime defined on the block at 39:45...
src/graph.rs:39 self.children(source, |target,_| {
src/graph.rs:40 if !visited.contains(&target) {
src/graph.rs:41 // visited.insert(target);
src/graph.rs:42 // self.dfs_rec(target, visitor, visited);
src/graph.rs:43 };
src/graph.rs:44 });
src/graph.rs:37:99: 45:6 note: ...does not necessarily outlive the anonymous lifetime #4 defined on the block at 37:98
src/graph.rs:37 fn dfs_rec<'a>(&self, source: &Node, visitor: |&Node| -> (), visited: &'a mut HashSet<&Node>) {
src/graph.rs:38 visitor(source);
src/graph.rs:39 self.children(source, |target,_| {
src/graph.rs:40 if !visited.contains(&target) {
src/graph.rs:41 // visited.insert(target);
src/graph.rs:42 // self.dfs_rec(target, visitor, visited);
...
error: aborting due to previous error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment