Skip to content

Instantly share code, notes, and snippets.

@arielb1
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arielb1/8960554 to your computer and use it in GitHub Desktop.
Save arielb1/8960554 to your computer and use it in GitHub Desktop.
stack example
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)
}
@arielb1
Copy link
Author

arielb1 commented Feb 12, 2014

main.rs:13:15: 13:21 error: cannot infer an appropriate lifetime due to conflicting requirements
main.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 found std::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 '

' failed at 'explicit failure', /tmp/tmp.1e4k6VbcBi/servo/src/compiler/rust/src/librustc/lib.rs:440

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment