Skip to content

Instantly share code, notes, and snippets.

@bblum
Last active December 21, 2015 03:39
Show Gist options
  • Select an option

  • Save bblum/6243839 to your computer and use it in GitHub Desktop.

Select an option

Save bblum/6243839 to your computer and use it in GitHub Desktop.
struct R<'self> {
// This struct is needed to create the
// otherwise infinite type of a fn that
// accepts itself as argument:
c: &'self mut &'self fn(&R, bool)
}
fn innocent_looking_victim() {
let mut x = Some(~"hello");
do conspirator |f, writer| {
if writer {
x = None;
} else {
match x {
Some(ref msg) => {
(*f.c)(f, true);
printfln!(msg);
},
None => fail!("oops"),
}
}
}
}
fn conspirator(mut f: &fn(&R, bool)) {
let r = R {c: &mut f};
f(&r, false)
}
fn main() { innocent_looking_victim() }
a.rs:40:18: 40:24 error: borrowed value does not live long enough
a.rs:40 let r = R {c: &mut f};
^~~~~~
a.rs:39:37: 42:1 note: borrowed pointer must be valid for the anonymous lifetime #1 defined on the block at 39:37...
a.rs:39 fn conspirator(mut f: &fn(&R, bool)) {
a.rs:40 let r = R {c: &mut f};
a.rs:41 f(&r, false)
a.rs:42 }
a.rs:39:37: 42:1 note: ...but borrowed value is only valid for the block at 39:37
a.rs:39 fn conspirator(mut f: &fn(&R, bool)) {
a.rs:40 let r = R {c: &mut f};
a.rs:41 f(&r, false)
a.rs:42 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment