Skip to content

Instantly share code, notes, and snippets.

Created August 25, 2017 06:07
Show Gist options
  • Save anonymous/5c4a7b0de00dcc6ec0612b8846dd6bfb to your computer and use it in GitHub Desktop.
Save anonymous/5c4a7b0de00dcc6ec0612b8846dd6bfb to your computer and use it in GitHub Desktop.
Rust code shared from the playground
use std::rc::Rc;
use std::cell::{Ref, RefCell};
struct Node<T> {
data: T,
}
struct Container<T> {
nodes: Vec<Rc<RefCell<Node<T>>>>,
}
impl<T> Container<T> {
fn new() -> Self {
Container { nodes: Vec::new() }
}
fn iter<'a>(&'a self) -> NodeIter<'a, T> {
NodeIter { iter: self.nodes.iter() }
}
}
struct NodeIter<'a, T: 'a> {
iter: std::slice::Iter<'a, Rc<RefCell<Node<T>>>>,
}
impl<'a, T: 'a> Iterator for NodeIter<'a, T> {
type Item = Ref<'a, T>;
fn next(&mut self) -> Option<Ref<'a, T>> {
self.iter.next().map(|rc| {
Ref::map(rc.borrow(), |node| &node.data)
})
}
}
fn main() {
let c: Container<()> = Container::new();
println!("Hello, world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment