Skip to content

Instantly share code, notes, and snippets.

@horndude77
Created October 26, 2013 05:19
Show Gist options
  • Select an option

  • Save horndude77/7165565 to your computer and use it in GitHub Desktop.

Select an option

Save horndude77/7165565 to your computer and use it in GitHub Desktop.
Attempting to understand rust pointers.
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