Skip to content

Instantly share code, notes, and snippets.

@lilyball
Created April 4, 2014 06:55
Show Gist options
  • Save lilyball/9969457 to your computer and use it in GitHub Desktop.
Save lilyball/9969457 to your computer and use it in GitHub Desktop.
use std::cell::{Ref,RefCell};
#[deriving(Show)]
struct Creature {
x: int
}
struct Foo {
creatures: Vec<RefCell<Creature>>
}
impl Foo {
fn creature_at<'a>(&'a self, x: int) -> Option<Ref<'a, Creature>> {
match self.creatures.iter().find(
|cr| match cr.try_borrow() {
Some(cr) => cr.x == x,
None => false
}
) {
Some(cr) => Some(cr.borrow()),
None => None
}
}
}
fn main() {
let foo = Foo {
creatures: vec!(RefCell::new(Creature { x: 3 }), RefCell::new(Creature { x: 4 }))
};
println!("{}", foo.creature_at(4).map(|c| *c));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment