Created
October 26, 2013 05:19
-
-
Save horndude77/7165565 to your computer and use it in GitHub Desktop.
Attempting to understand rust pointers.
This file contains hidden or 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); | |
| }, | |
| } | |
| } | |
| // The following function results in these errors: | |
| // test.rs:20:4: 20:19 error: cannot move out of dereference of & pointer | |
| // test.rs:20 Node{val, next} => { | |
| // ^~~~~~~~~~~~~~~ | |
| // test.rs:20:4: 20:19 error: cannot move out of dereference of & pointer | |
| // test.rs:20 Node{val, next} => { | |
| // ^~~~~~~~~~~~~~~ | |
| // error: aborting due to 2 previous errors | |
| /* | |
| fn print_list2<T:ToStr>(list: &List<T>) { | |
| match *list { | |
| End => { println("<X>"); }, | |
| Node{val, next} => { | |
| println(val.to_str()); | |
| print_list2(next); | |
| }, | |
| } | |
| } | |
| */ | |
| fn main() { | |
| let list = ~Node{ val: 0, next:~Node{ val: 1, next:~End } }; | |
| print_list(*list); | |
| //print_list2(list); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment