There is no need to clone, it is absolutely possible to do what you want to get with links:
use std::rc::Rc; enum NodeKind { Branch(Rc<Node>), Leaf, } struct Node { value: i32, kind: NodeKind, } fn main() { let leaf = Node { value: 10, kind: NodeKind::Leaf }; let branch = Node { value: 50, kind: NodeKind::Branch(Rc::new(leaf)) }; let root = Node { value: 100, kind: NodeKind::Branch(Rc::new(branch)) }; let mut current = &root; loop { println!("{}", current.value); match current.kind { NodeKind::Branch(ref next) => { current = &**next; } NodeKind::Leaf => break, } } }
The only important changes to your code are that the pattern in the match ref next and current is of type &Node .
ref templates bind their variables by reference, that is, next is of type &Rc<Node> . To get &Node from it, you need to dereference it twice to get Node , and then get &Node again. Due to coercion, Deref can also write current = &next , and the compiler automatically inserts the corresponding number * .
I also changed from while (true) to loop , because it is more idiomatic and helps the compiler talk about your code.
All tree structure walks are performed as follows in Rust. ref templates allow you to not move from variables, which is absolutely necessary when you only need to read data. You can find more about templates and how they interact with ownership and borrowing here .
source share