struct Node { data : T, left : Option>>, right : Option>>, } impl Node { pub fn new(data : T) -> Self { Node{ data, left: None, right: None, } } } fn main() { // Setup a binary tree let mut parent = Box::new(Node::new(15)); parent.left = Some(Box::new(Node::new(7))); if let Some(ref mut left) = parent.left { left.left = Some(Box::new(Node::new(3))); } parent.right = Some(Box::new(Node::new(30))); use std::collections::VecDeque; // Loop through a binary tree let mut stack = Vec::new(); let mut current = Some(&parent); while current.is_some() || !stack.is_empty() { while current.is_some() { stack.push(current); current = current.unwrap().left.as_ref(); } current = stack.pop().unwrap(); dbg!(current.unwrap().data); current = current.unwrap().right.as_ref(); } println!("Still can use parent here!"); dbg!(parent.data); }