Skip to content

Instantly share code, notes, and snippets.

@arkadijs
Last active August 29, 2015 14:23
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 arkadijs/933c70751629f352e4b0 to your computer and use it in GitHub Desktop.
Save arkadijs/933c70751629f352e4b0 to your computer and use it in GitHub Desktop.
Beauty of Rust
#![allow(dead_code, unused_variables)]
#[derive(Debug)]
struct Node<'l, 'r> {
val: u32,
left: Option<&'l Node<'l, 'l>>,
right: Option<&'r Node<'r, 'r>>
}
impl<'l, 'r> Node<'l, 'r> {
fn leaf(val: u32) -> Node<'l, 'r> {
Node { val: val, left: None, right: None }
}
fn new(val: u32, left: &'l Node<'l, 'l>, right: &'r Node<'r, 'r>) -> Node<'l, 'r> {
Node { val: val, left: Some(left), right: Some(right) }
}
}
enum Dir {
Left, Right
}
struct Choice {
choice: Dir
}
fn choose<'n>(node: &'n Node<'n, 'n>, what: &Choice) -> Option<&'n Node<'n, 'n>> {
match what.choice {
Dir::Left => node.left,
Dir::Right => node.right
}
}
fn main() {
let left = Node::leaf(1);
let right = Node::leaf(2);
let root = Node::new(3, &left, &right);
let result = choose(&root, &Choice { choice: Dir::Right });
println!("\nresult: {:?}", result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment