Skip to content

Instantly share code, notes, and snippets.

@stevenblenkinsop
Created October 26, 2013 06:05
Show Gist options
  • Save stevenblenkinsop/7165828 to your computer and use it in GitHub Desktop.
Save stevenblenkinsop/7165828 to your computer and use it in GitHub Desktop.
Demonstration of ref patterns for avoiding moving out of &
enum List<T> {
End,
Node { val: T, next: ~List<T> },
}
fn print_list<T:ToStr>(list: List<T>) {
match list {
End => { println("<X>"); },
Node{val, next} => {
println(val.to_str());
print_list(*next);
},
}
}
// Need to take references to the fields so they
// don't get moved.
fn print_list2<T:ToStr>(list: &List<T>) {
match *list {
End => { println("<X>"); },
Node{val: ref val, next: ref next} => {
println(val.to_str());
print_list2(*next);
},
}
}
fn main() {
let list = ~Node{ val: 0, next:~Node{ val: 1, next:~End } };
print_list2(list);
print_list(*list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment