Last active
August 29, 2015 13:56
-
-
Save arielb1/8960554 to your computer and use it in GitHub Desktop.
stack example
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
struct stack<'a> { | |
next: Option<&'a stack<'a>>, | |
data: i32 | |
} | |
fn doit(st: &stack) -> () { | |
if(st.data == 0) { | |
let mut cur = Some(st); | |
loop { match cur { | |
None => { break; } | |
Some(u) => { | |
println(u.data.to_str()); | |
cur = u.next; | |
} | |
}} | |
} else { | |
let blk = stack { next: Some(st), data: st.data-1 }; | |
doit(&blk) | |
} | |
} | |
fn main() -> () { | |
let u = stack { next: None, data: 5 }; | |
doit(&u) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
main.rs:13:15: 13:21 error: cannot infer an appropriate lifetime due to conflicting requirements
' failed at 'explicit failure', /tmp/tmp.1e4k6VbcBi/servo/src/compiler/rust/src/librustc/lib.rs:440main.rs:13 cur = u.next;
^~~~~~
main.rs:19:5: 20:4 note: first, the lifetime cannot outlive the call at 19:4...
main.rs:19 doit(&blk)
main.rs:20 }
main.rs:19:10: 19:14 note: ...so that argument is valid for the call
main.rs:19 doit(&blk)
^~~~
main.rs:6:27: 21:2 note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the block at 6:26...
main.rs:6 fn doit(st: &stack) -> () {
main.rs:7 if(st.data == 0) {
main.rs:8 let mut cur = Some(st);
main.rs:9 loop { match cur {
main.rs:10 None => { break; }
main.rs:11 Some(u) => {
...
main.rs:13:15: 13:21 note: ...so that types are compatible (expected
std::option::Option<&stack<>>
but foundstd::option::Option<&stack<>>
)main.rs:13 cur = u.next;
^~~~~~
error: aborting due to previous error
task 'rustc' failed at 'explicit failure', /tmp/tmp.1e4k6VbcBi/servo/src/compiler/rust/src/libsyntax/diagnostic.rs:74
task '