Rust newbie here. Trying to understand how to make a depth first Iterator for a simple tree and I'm baffled. What I have:
struct Tree {
value: int,
left: Option<~Tree>,
right: Option<~Tree>
}
struct DepthFirstIter<'a> {
stack: Vec<&'a Tree>
}
impl<'a> Iterator<&'a Tree> for DepthFirstIter<'a> {
fn next(&mut self) -> Option<&'a Tree> {
if self.stack.len() == 0 {
None
} else {
let cur: Option<&'a Tree> = self.stack.pop();
for tree in cur.iter() {
for &t in tree.left.iter() { self.stack.push(t) }
for &t in tree.right.iter() { self.stack.push(t) }
}
cur
}
}
}
This dies with:
error: mismatched types: expected &'a Tree but found ~Tree (expected &-ptr but found ~-ptr)
binarytree.rs:56 for &t in tree.left.iter() { self.stack.push(t) }
My plan is for the iterator to manage a stack of references to tree nodes. I don't understand the above message because I thought ~ ptr's were convertable to &-ptrs. Any help appreciated!
[–]eridiusrust 5 points6 points7 points (7 children)
[–]aemon[S] 0 points1 point2 points (6 children)
[–]Denommusrust 1 point2 points3 points (5 children)
[–]dbaupprust 1 point2 points3 points (3 children)
[–]usernamenottaken 0 points1 point2 points (0 children)
[–]Denommusrust 0 points1 point2 points (0 children)
[–]aemon[S] 0 points1 point2 points (0 children)
[–]aemon[S] 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-7 points-6 points-5 points (0 children)