Created
October 26, 2013 06:05
-
-
Save stevenblenkinsop/7165828 to your computer and use it in GitHub Desktop.
Demonstration of ref patterns for avoiding moving out of &
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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